AVAssetWriter / AVAudioPlayer 冲突?

Posted

技术标签:

【中文标题】AVAssetWriter / AVAudioPlayer 冲突?【英文标题】:AVAssetWriter / AVAudioPlayer Conflict? 【发布时间】:2011-07-23 21:12:41 【问题描述】:

几周前,我发布了这个关于我在使用 AVAssetWriter 时遇到的问题的帖子:AVAssetWriter Woes

进一步的研究似乎导致在使用 AVAudioPlayer 或实际上任何音频系统播放音频时使用 AVAssetWriter 的冲突。我也尝试过使用 OpenAL。

这是背景:

使用 AVAssetWriter 将帧从一个图像或一组图像写入视频可以正常工作,直到调用 [AVAudioPlayer play]。

这只会发生在设备上,不会发生在 SIM 卡上。

尝试从 CVPixelBufferPoolCreatePixelBuffer 创建像素缓冲区时发生错误。

音频开始播放后,之前存在的 AVAssetWriterInputPixelBufferAdaptor.pixelBufferPool 突然变为 nil。

您可以在这里下载代表项目:http://www.mediafire.com/?5k7kqyvtbfdgdgv

注释掉 AVAudioPlayer 播放,它将在设备上运行。

感谢任何线索。

【问题讨论】:

【参考方案1】:

我已经找到了解决这个问题的方法。

如果您想让 AVAudioPlayer 和 AVAssetWriter 一起正常运行,您必须拥有“可混合”的音频会话类别。

您可以使用可混合的类别,例如 AVAudiosessionCategoryAmbient。

但是,我需要使用 AVAudioSessionCategoryPlayAndRecord。

您可以通过实现此功能将任何类别设置为可混合:

OSStatus propertySetError = 0;

UInt32 allowMixing = true;

propertySetError = AudioSessionSetProperty (
                       kAudioSessionProperty_OverrideCategoryMixWithOthers,  // 1
                       sizeof (allowMixing),                                 // 2
                       &allowMixing                                          // 3
                   );

【讨论】:

【参考方案2】:

以上答案是完整的。它不起作用。改为这样做

// Setup to be able to record global sounds (preexisting app sounds)
    NSError *sessionError = nil;
    if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(setCategory:withOptions:error:)])
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:&sessionError];
    else
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

    // Set the audio session to be active
    [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];

//then call your asset writer 
movieWriter = [[AVAssetWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];

【讨论】:

以上是关于AVAssetWriter / AVAudioPlayer 冲突?的主要内容,如果未能解决你的问题,请参考以下文章

AVAssetWriter 未知错误

AVAssetWriter - 设置自定义帧率

AVAssetWriter 元数据日期问题

AVAssetWriter 文件备份与恢复

我可以使用 AVAssetWriter 代替 AVExportSession 吗?

是否可以使用 AVAssetWriter 完全裁剪视频?