【问题标题】:AVAudioPlayer refuses to play anything, but no error, etcAVAudioPlayer 拒绝播放任何东西,但没有错误等
【发布时间】:2012-02-20 14:05:30
【问题描述】:

这是最简单的 AVAudioPlayer 代码,只是无法播放任何内容。没有错误,控制台中什么都没有,肯定会找到该文件,就好像我将 URL 字符串更改为不存在的东西一样,我确实遇到了崩溃。我在这里做错了什么?我在有和没有委托的情况下,以及有和没有 prepareToPlay 的情况下都试过了,但我什么都没有。我也尝试过各种声音文件。真的把我的头发扯掉了!

@implementation ViewController

- (IBAction)playSound:(id)sender
{
    NSURL *soundLocation = [[NSURL alloc] initWithString:@"Lot01.wav"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundLocation error:nil];
    [audioPlayer setDelegate:self];
    [audioPlayer play];
}

@end

【问题讨论】:

    标签: objective-c avaudioplayer wave


    【解决方案1】:

    原来这是 ARC 发布的一个问题,我通过为有问题的 AVAudioPlayer 添加 @property 和 @synthesize 对来修复它,并声明它是强大的。它消除了所有这些错误,并且毫无问题地播放了文件。

    【讨论】:

      【解决方案2】:

      似乎无法正确获取文件的路径,不妨试试这样的方法。

      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Lot01" ofType:@"wav"];
      NSURL *url = [NSURL fileURLWithPath:filePath];
      

      然后完全按照你的方式初始化 AVAudioPlayer,除非在这种情况下它会是 withContentsOfURL:url。

      【讨论】:

      • 好的,执行此操作时,仍然没有音频输出,但在单击按钮触发 IBAction 时会有短暂的停顿,这表明正在发生某些事情。单击时我也得到了很多控制台输出,还有很多关于我安装的不相关的音频单元插件的错误。我需要告诉 iPhone Simulator 使用哪个音频输出设备,还是使用系统默认设置?我也在设备上测试过,也没有声音。
      猜你喜欢
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2013-03-22
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多