AVAudioPlayer没有响应

问题描述:

我初始化AVAudioPlayer实例:AVAudioPlayer没有响应

NSString* path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"wav"]; 

AVAudioPlayer* player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; //Returned object not nil, indicating that the file has loaded successfully 


BOOL b = [player prepareToPlay]; //returns TRUE 

BOOL b2 = [player play]; 
//returns TRUE, goes immediately to the next line since asynchronous 


[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]; //wait 1 sec 

printf("%10.4f\n",player.duration); //duration is 2s 

printf("%10.4f\n",player.currentTime); //position remains 0 

/* There is no sound output, player is also stuck since position==0 */ 

有谁知道为什么AVAudioPlayer没有响应?在初始化,播放音频播放器时,是否有一些我可以忽略?样板代码可能?

我在iPhone模拟器上运行这个。此外,AudioServicesPlaySystemSound()适用于相同的文件,这令人费解,但这表明声音播放也可能适用于AVAudioPlayer。

我有2点快速建议:看

  1. 检查,如果这条道路,其实是给予你的数据。使用[[NSData alloc] initWithContentsOfFile:path]分配一个NSData对象,并在调试器中检查它是否实际加载该wav文件。

  2. 我写了一个示例项目,在这里使用AVAudioPlayer:AVAudioPlayer Example。由于代码与您的代码非常相似,我可以想象的唯一情况是您的数据存在问题。

检查出来,看看它是否让你任何地方!

我刚刚有一个类似的问题。我不确定它是否是相同的解决方案,但我的应用程序通过iphone记录音频,然后将其播放回用户。

我以为我告诉我的音频会话,它是要记录的数据做了正确的事情,所以我做了以下内容:

[audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; 

使用该设置播放制作的模拟器,但不上设备.... didFinishPlaying事件永远不会升起。

我意识到我不需要它,音频播放开始正常工作后,我删除了这一行。现在我只需要弄清楚为什么它如此安静:)

保罗