ios - AVAudioPlayer:简单的声音不播放

标签 ios avaudioplayer

我试图弄清楚为什么我的应用程序中没有播放声音,所以我创建了一个我认为尽可能简单的实现:

  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/

相关文章:

c++ - 使用 QSoundEffect 时选择音频输出驱动

ios - iOS 应用程序上的 DiskCookies 崩溃是什么意思?

ios - GCM iOS 生产环境

ios - 播放大量音频文件后,AVAudioPlayer停止播放:kAudio_TooManyFilesOpenError

ios - AVAudioPlayer 导致应用程序崩溃

ios - 不使用 AVAudioPlayer 播放 mp3 文件

iphone - Uipopover框架在方向上重置而不隐藏或关闭?

iphone - iPhone中的UIWebView全屏视频

ios - 自定义 MPMoviePlayerController 中的 UI slider ?

ios - 如何同时录制视频和播放音频(swift教程)