2013-04-22 51 views
0

这是一个新手问题。iOS按钮无法播放按钮水龙头上的音频文件

我有一个非常简单的应用程序是应该当主视图按钮被窃听只播放音频文件。

我使用的XCode 4.6版。

我增加了一个音频文件“audioclip.wav”我的项目。我已添加AVFoundation.framework

我的项目只有ViewController.hViewController.m

我双击并使用助理编辑器创建我的IBAction连接从按钮拖在故事板.h文件。

我ViewController.h:

#import <UIKit/UIKit.h> 
#import<AVFoundation/AVAudioPlayer.h> 

@interface ViewController : UIViewController <AVAudioPlayerDelegate> { 
} 
- (IBAction)playAudio:(id)sender; 

@end 

我ViewController.m:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)playAudio:(id)sender { 
    AVAudioPlayer *audioPlayer; 
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audioclip" ofType:@"wav" ]; 
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; 
    [audioPlayer play]; 
} 

@end 

无论出于何种原因(可能是一些愚蠢的我离开了)的audioclip.wav文件不,当我点击播放按钮。我已经在iPhone 6.1模拟器和我的设备上进行了测试。

+0

检查IBOutlet中和按钮操作? – Balu 2013-04-22 04:18:05

+0

@ brno792检查您是否在TouchUpInside方法上连接了按钮。 – 2013-04-22 04:21:28

+0

看到我的答案.. http:// stackoverflow。com/questions/16033723/audio-file-not-working-on-device-but-it-is-working-on-simulator/16033796#16033796 – 2013-04-22 04:23:12

回答

6

我认为有一些问题,wav文件。有些人似乎开始工作,但我已经尝试了几个不同的文件,而且他们都没有播放。在下面的代码中注释掉的mp3和m4a文件运行良好。没有必要对委托做任何事情,这是可选的。

#import "ViewController.h" 
    #import <AVFoundation/AVFoundation.h> 

    @interface ViewController() 
    @property (strong,nonatomic) AVAudioPlayer *audioPlayer; 
    @end 

    @implementation ViewController 


    - (IBAction)playAudio:(id)sender { 
     //NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"11 Prayer" ofType:@"mp3" ]; 
     //NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"Sunshine" ofType:@"m4a" ]; 
     NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"space" ofType:@"wav" ]; 
     NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 
     NSError *error; 
     self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error]; 
     [self.audioPlayer play]; 
    } 

如果我在错误日志中与wav文件,我得到:

Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn’t be completed. (OSStatus error 1685348671.)" 
+0

Thankyou @rdelmar – brno792 2013-04-22 05:26:00

+0

@ brno792,你真的明白了吗?如果是这样,怎么样?我想知道。 – rdelmar 2013-04-22 05:29:29

+0

我没有@property所以也许就是这样。或者我有错误=零,而不是错误=&错误,所以也许这也是它。它现在有用,所以谢谢。我仍然使用.wave。如果我想在按下按钮时随机选择一个音频文件从3或4播放,我该怎么办?谢谢。 – brno792 2013-04-22 05:32:21

0

尝试这样,

 NSString *audio=[[NSBundle mainBundle]pathForResource:@"audioclip" ofType:@"wav"]; 
    NSURL *url=[[NSURL alloc]initFileURLWithPath:audio]; 
    avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 
    avPlayer.delegate = self; 
    [avPlayer prepareToPlay]; 
    [avPlayer play]; 

和检查一次IBOutlet连接与否和使用断点检查一下按键的操作方法。

+0

只是方法调用iboutlet不是强制的,而是适当的事件应该触发。 – 2013-04-22 04:20:50

+0

谢谢,所以我尝试了你的解决方案。我不得不添加AVAudioPlayer * avPlayer = ....我也检查了IBOutlet,我收到了操作:playAudio:Button Touch Up Inside。所以我认为它是连接的。我仍然没有听到任何声音。 – brno792 2013-04-22 05:01:36

+0

一旦检查您的音频文件是否有任何音乐或不/ /? – Balu 2013-04-22 05:03:07

1

请尽量使用这一个..

- (IBAction)playAudio:(id)sender 
{ 
    NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"audioclip" ofType:@"wav"]]; 
    NSData *data =[NSData dataWithContentsOfURL:url]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; 
    audioPlayer.delegate = self; 
    [audioPlayer setNumberOfLoops:0]; 

    [audioPlayer play]; 
} 

我希望这将帮助ü

0

,我可以看到你所做的代码非常简单,所以它的制作方式确实需要它不一个大的wav文件。

和保存您需要的音频文件削减到像10秒下。

0

我遇到这个问题时使用了.m4a。对我来说,不执行AVAudioSession是问题

VAR会话= AVAudioSession.sharedInstance()

do { 
     session.setCategory(AVAudioSessionCategoryPlayAndRecord) 
     try session.setActive(true) 
    } catch { 
     gf.displayAlert("problem encountered", userMessageTitle: "") 
     self.navigationController?.popViewControllerAnimated(true) 
    }