【问题标题】:AVAudioPlayer on OS X not playing any soundOS X 上的 AVAudioPlayer 不播放任何声音
【发布时间】:2015-02-26 11:57:45
【问题描述】:

我正在尝试在 os x(不是 iOS!)上使用 AVAudioPlayer 播放声音。我正在使用以下代码:

AVAudioPlayer *player;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
          soundFileURL error:&err];

if( err ){
    NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
    player.delegate = self;
    player.numberOfLoops = -1;
    player.currentTime = 0;
    player.volume = 1.0;
    [player play];
}

一切正常,播放器调用播放函数没有错误。但是在那之后我就听不到任何声音了。我确定我的音频文件没问题,因为我可以使用NSSound 类播放它。我想使用AVAudioPlayer 的原因是,当我使用NSSound 类并循环播放时,中间会有短暂的停顿,这就是我想尝试AVAudioPlayer 的原因。

【问题讨论】:

  • 将 *player 定义为属性,以便在您的方法完成后它不会消失?
  • 非常感谢。就是这样-如果您将评论写为答案,我会接受。

标签: objective-c macos cocoa avaudioplayer


【解决方案1】:

*player 定义为property,这样它就不会在您的方法完成后消失。

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多