AVAssetReader 读取音频,然后失败

2024-02-19

我的应用程序读取音频并在生产者/消费者设置中播放它。消费者线程请求新样本渲染到硬件。生产者线程使用 AVAssetReader 将音频数据从磁盘读取到其缓冲区中。生产者线程循环运行,检查是否需要读取更多样本。制作者的缓冲区大小等于 4 秒的音频。

当我指示我的应用程序缓冲音频时,样本会成功读取,没有错误。当我触发制作者线程开始渲染音频时,4 秒缓冲区会完美播放,然后静音。进一步的调查显示,我的资产读取器在开始播放时失败,因此在初始缓冲后不再读取进一步的样本:

CMSampleBufferRef ref = [readaudiofile copyNextSampleBuffer];
if (ref == NULL && filereader.status == AVAssetReaderStatusFailed) {
    NSLog(@"reader failed: %@", filereader.error);
}
// this produces: 
// {NSLocalizedFailureReason=An unknown error occurred (-12785), 
// NSUnderlyingError=0x161f60 "The operation couldn’t be completed. 
// (OSStatus error -12785.)", NSLocalizedDescription=The operation 
// could not be completed}

我的生产者线程代码与功能代码示例相同音乐库远程IO演示 http://www.cogs.susx.ac.uk/users/nc81/code.html。我的消费者线程代码是不同的,但我已经将两者逐行比较了几天,似乎找不到问题。知道什么可能导致 AVAssetReader 阅读器失败吗?

这个帖子 https://stackoverflow.com/questions/4763598/avassetreader-and-audio-queue-streaming-problem描述了类似的解决方案,并得出结论,需要正确设置音频会话(尽管没有说明如何设置)。 AVAudioSesion 配置和 AVAssetReader 的行为之间有任何联系吗?


我遇到了同样的错误,我也在另一个线程上发布了这个答案

- (void)setupAudio {
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error:&activationError];

    NSLog(@"setupAudio ACTIVATION ERROR IS %@", activationError);
    [[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.1 error:&activationError];
    NSLog(@"setupAudio BUFFER DURATION ERROR IS %@", activationError);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AVAssetReader 读取音频,然后失败 的相关文章

随机推荐