AVAssetReader读取音频,然后失败 [英] AVAssetReader reads audio, then fails

查看:336
本文介绍了AVAssetReader读取音频,然后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app reads audio and plays it back in a producer / consumer setup. The consumer thread requests new samples to render to hardware. The producer thread reads audio data from disk into its buffer using AVAssetReader. The producer thread runs in a loop, checking if more samples need to be read. The producer's buffer size is equal to 4 seconds of audio.

当我指示我的应用程序缓冲音频时,可以成功读取样本,而不会出现错误.当我触发生产者线程开始渲染音频时,会完美播放4秒缓冲区,然后静音.进一步的调查表明,我的资产读取器在播放开始时发生故障,因此在初始缓冲后读取的样本不再:

When I instruct my app to buffer audio, samples are read successfully without error. When I trigger my producer thread to begin rendering audio, the 4 second buffer is played back perfectly, then silence. Further investigation reveals that my asset reader fails upon the start of playback, thus no further samples where read after the initial buffering:

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}

我的生产者线程代码与功能代码示例 MusicLibraryRemoteIODemo .我的使用者线程代码不同,但是我已经连续两天比较了这两个线程,而且似乎找不到障碍.任何想法可能导致AVAssetReader阅读器失败吗?

My producer thread code is identical to the functional code example MusicLibraryRemoteIODemo. My consumer thread code is different, but I've compared the two line by line for a couple days and can't seem to find the hitch. Any idea what might be causing the AVAssetReader reader to fail?

这篇文章描述了类似的解决方案,并得出结论认为音频会话需要进行正确设置(尽管它没有说明如何操作). AVAudioSesion配置与AVAssetReader的行为之间是否存在任何联系?

This post describes similar solutions and it concluded that the Audio Session needed to be set up properly (although it doesn't say how). Is there any connection between AVAudioSesion configuration and the behavior of AVAssetReader?

推荐答案

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

I was getting this same error, I posted this answer on the other thread as well

- (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);
}

这篇关于AVAssetReader读取音频,然后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆