我试图弄清楚为什么我的应用程序中没有播放声音,所以我创建了一个我认为尽可能简单的实现:
NSError *error;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"canary-trills" ofType:@"wav"];
NSLog(@"string=%@", soundFilePath);
NSURL *url = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSLog(@"URL=%@", url);
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url error:&error];
[avPlayer prepareToPlay];
BOOL success = [avPlayer play];
NSLog(@"The sound %@ play", success ? @"did" : @"didn't");
根据控制台,它似乎找到了资源,正确地创建了 URL。即使是 play call 也表示成功。但是,模拟器和设备中都没有声音播放。
最佳答案
AVAudioPlayer 未被保留且超出范围。
添加: @property (strong, nonatomic) AVAudioPlayer *audioPlayer;
到类(class)并使用该成员解决了问题。
关于ios - AVAudioPlayer:简单的声音不播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314363/