简体   繁体   English

AVAssetWriter / AVAudioPlayer冲突?

[英]AVAssetWriter / AVAudioPlayer Conflict?

Weeks ago, I posted this thread regarding problems I was having with AVAssetWriter: AVAssetWriter Woes 几周前,我发布了有关我与AVAssetWriter遇到的问题的线程: AVAssetWriter Woes

Further research seems to lead to a conflict using AVAssetWriter while playing audio with AVAudioPlayer or, really, any audio system. 进一步的研究似乎导致在使用AVAudioPlayer或实际上任何音频系统播放音频时使用AVAssetWriter引起冲突。 I tried with OpenAL as well. 我也尝试过OpenAL。

Here's the background: 这是背景:

  • Using AVAssetWriter to write frames to a video from an image or set of images works fine UNTIL [AVAudioPlayer play] is called. 使用AVAssetWriter从图像或一组图像向视频中写入帧可以正常工作,直到调用[AVAudioPlayer播放]。

  • This only happens on the device, not the sim. 这仅在设备上发生,而不在SIM卡上发生。

  • The error occurs when attempting to create a pixel buffer from CVPixelBufferPoolCreatePixelBuffer. 尝试从CVPixelBufferPoolCreatePixelBuffer创建像素缓冲区时发生错误。

  • Once the audio starts playing, the AVAssetWriterInputPixelBufferAdaptor.pixelBufferPool which existed before suddely becomes nil. 音频开始播放后,突然出现的AVAssetWriterInputPixelBufferAdaptor.pixelBufferPool变为零。

You can download the representative project here: http://www.mediafire.com/?5k7kqyvtbfdgdgv 您可以在此处下载代表性项目: http : //www.mediafire.com/?5k7kqyvtbfdgdgv

Comment out AVAudioPlayer play and it will work on the device. 注释掉AVAudioPlayer的播放,它将在设备上运行。

Any clues are appreciated. 任何线索表示赞赏。

This above answer is in complete. 以上答案已经完成。 It doesn't work. 没用 Do this instead 改为这样做

// 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)];

I've found the solution to this issue. 我已经找到解决此问题的方法。

If you want to have AVAudioPlayer and AVAssetWriter behave correctly together, you must have and audio session category that is 'mixable'. 如果要使AVAudioPlayer和AVAssetWriter一起正常运行,则必须具有“可混合”的音频会话类别。

You can use a category that is mixable like AVAudioSessionCategoryAmbient. 您可以使用可混合使用的类别,例如AVAudioSessionCategoryAmbient。

However, I needed to use AVAudioSessionCategoryPlayAndRecord. 但是,我需要使用AVAudioSessionCategoryPlayAndRecord。

You can set any category to be mixable by implementing this: 您可以通过实现以下方式将任何类别设置为可混合使用:

OSStatus propertySetError = 0;

UInt32 allowMixing = true;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM