【问题标题】:How to deal with AVAudioPlayer correctly in objective-c如何在objective-c中正确处理AVAudioPlayer
【发布时间】:2012-12-29 14:51:04
【问题描述】:

我正在开发小游戏,但背景音乐有问题。我使用AVAudioPlayer 播放循环音乐。如下所示:

NSString *path = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"ogg"];
NSError *error;

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:&error];

NSLog(@"%@", [error description]);

[player setNumberOfLoops:-1];
player.delegate = self;
[player play];

我已经进入Supproting Files 文件background.oggbackground.mp3background.wav 并且没有人播放。它有什么问题?

当我使用NSLog 打印描述错误时,我得到了:

Error Domain=NSOSStatusErrorDomain Code=1954115647 "The operation couldn’t be completed. (OSStatus error 1954115647.)"

请帮忙。

【问题讨论】:

  • 这是kAudioFileUnsupportedFileTypeError 的代码。但是iOS绝对可以播放MP3和WAV文件,最多不能处理OGG文件。我怀疑问题在于您应该使用+ [NSURL fileURLWithPath:] 而不是URLWithString:,因为文件路径本身不是有效的URL。
  • @H2CO3, error = (null) 当我使用 .mp3 和 + [NSURL fileURLWithPath:] 但仍然没有声音。

标签: objective-c avfoundation avaudioplayer background-music


【解决方案1】:

有时候,只是因为你的音频文件的质量不符合它需要的采样率,请检查你的音频采样率是否低于24kHz。 我测试了一些音频,我发现当音频采样率高于 44kHz 时,它工作得很好,但是如果它低于 44kHz 和高于 22kHz,AudioToolbox 不能先播放声音,而 AVAudioPlayer 可以工作一些次。如果低于 22kHz,它们都将无法播放这些音频文件。(我测试的这些音频文件格式只是“m4a”、“mp3”、“wav”)

顺便说一句,如果你想将低采样率的音频转换为 44kHz 的“m4a”文件,用 iTunes 也是不行的。不要在这上面浪费你的时间。

【讨论】:

    【解决方案2】:

    这太容易了。还有一件事。当您使用例如 NSTimer 时,您必须在某个地方使用 ivalidate 它。当您使用AVAudioPlayer 时,您必须在某个地方使用stop

    [player stop];
    

    【讨论】:

    • 在你想停止音乐的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多