AVAssetReader 读取音频,然后失败

发布于 2024-11-02 21:55:18 字数 1129 浏览 6 评论 0原文

我的应用程序读取音频并在生产者/消费者设置中播放它。消费者线程请求新样本渲染到硬件。生产者线程使用 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}

我的生产者线程代码与功能代码示例相同 MusicLibraryRemoteIODemo。我的消费者线程代码是不同的,但我已经将两者逐行比较了几天,似乎找不到问题。知道什么可能导致 AVAssetReader 阅读器失败吗?

这篇文章描述了类似的解决方案,得出的结论是需要设置音频会话正确地(虽然它没有说明如何)。 AVAudioSesion 配置和 AVAssetReader 的行为之间有任何联系吗?

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.

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}

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-11-09 21:55:18

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

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文