2012-09-21 39 views
3

我有一个非常简单的应用程序,我已经建立了测试AVAudioPlayer。我有一个16s aif文件,可以在我的Mac上播放。把它放在一个简单的应用程序,并在我的iPhone上运行,但没有声音。向对象发送'play'会返回成功,并且16秒后audioPlayerDidFinishPlaying成功返回。仍然没有声音。已经检查过iPhone上的音量,很好。已经检查过,以确保我的AVAudioPlayer对象的音量设置为1.0 - 它确实如此。任何人都可以帮忙吗?AVAudioPlayer返回成功,但没有声音从设备

这里是我的代码

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Soothing" ofType:@"aif"]; 
NSURL *playURL = [NSURL fileURLWithPath:soundPath]; 
NSError *error; 

self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error]; 
NSLog(@"About to play sound"); 

self.myAudioPlayer.delegate = self; 
[self.myAudioPlayer prepareToPlay]; 
bool success = [self.myAudioPlayer play]; 

if (!success) { 
    NSLog(@"Failed to play file"); 
} else NSLog(@"Looks like it succeded"); 

回答

8

好的 - 知道了。我没有为我的应用程序配置AVAudioSession。做了一件很简单如下(从苹果的示例代码复制):

[[AVAudioSession sharedInstance] setDelegate: self]; 
NSError *setCategoryError = nil; 
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError]; 
if (setCategoryError) 
    NSLog(@"Error setting category! %@", setCategoryError); 

声音现在效力 - 加上学到了很多关于如何通过屏幕锁等,这将是有价值的控制播放。

0

你检查,如果你从错误什么?

if(error) { 
    NSLog(@"%@", [error localizedDescription]); 
} 
+0

没有这样的运气 - 错误是零 – CodeBuddy