ios - AVAudioPlayer突然不再播放整个音频源

标签 ios objective-c xcode audio avaudioplayer

我正在使用AVAudioPlayer在我的应用程序中播放音频,并且运行了一段时间。突然,它只是停止工作了,我不知道为什么。我在.h文件的类级别创建了AVAudioPlayer。这是我用来初始化播放器的代码

-(void)initAudio
 {
    NSURL *song = [[NSBundle mainBundle] URLForResource:@"Prelude" withExtension:@"m4a"];
    NSError *error = nil;
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:song error:&error];
    [_audioPlayer prepareToPlay];
}

我在initWithSize函数中调用此方法。错误为零,并且我已经验证播放器对象包含实际的音频文件。音频文件在我的OS环境中可以正常播放,并且为了安全起见,我尝试将其添加到构建阶段的编译源中,即使事先在没有显式音频文件的情况下也可以正常工作。当我触发播放事件时,它将播放音频的第一个片段,然后不播放任何内容。我记录了播放器的当前时间,它是0。有关该应用程序的所有其他信息仍在继续,并且可以正常运行。除了播放/暂停/ preparetoplay,我不会以任何方式干扰avaudioplayer对象。

由于工作正常,我完全感到困惑,然后突然不再工作。我曾尝试清理该项目,甚至已转储了导出数据。

有任何想法吗?

最佳答案

我认为您尝试播放歌曲时,播放器出现错误。

您需要先检查错误。

在我看来,这似乎是OSStatus错误-43。

更新应用程序时,可以更改文件路径。

例如,
旧路径:/var/mobile/Applications/F28E0C2C-4AE2-4C9D-906F-414AE1669D90/Documents/AAA.mp3

新路径:var / mobile / Applications / 6086783B-A410-42C3-9BAE-7BA755D0E528 / Documents / AAA.mp3

请尝试以下代码:

 NSError *error;
 NSURL *song = [[NSBundle mainBundle] URLForResource:@"Prelude" withExtension:@"m4a"];

_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:song error:&error];

if(error){

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

    NSString *fileName = [[song absoluteString] lastPathComponent]; //this code is just example. get your fileName like "AAA.m4a"

    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirPath , fileName];

    _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:filePath] error:&error];

    }

    _audioPlayer.delegate = self;

    [_audioPlayer prepareToPlay];

    [_audioPlayer play];

}

我希望这能帮到您。

干杯

关于ios - AVAudioPlayer突然不再播放整个音频源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566069/

相关文章:

iOS 键盘 - 大写 'done' 按钮

xcode - 隐藏 Swift "Will never be executed"警告

ios - PrepareForSegue 问题

ios - 在 Sqlite iOS 中找不到表

ios - 具有内置邮件外观的自定义 UIActivity

ios - 更新后 Xcode 中 app.dsym 中的警告

ios - 以编程方式禁用 iOS "Perspective Compensation"?

objective-c - RestKit iOS 应用程序根据请求崩溃

xcode - Swift 语言中的命名空间和模块

ios - Xcode 4 : Exceptions not being logged to the console