Search code examples
iphoneiosaudioavaudioplayeravassetwriter

AVAssetWriter / AVAudioPlayer Conflict?


Weeks ago, I posted this thread regarding problems I was having with AVAssetWriter: AVAssetWriter Woes

Further research seems to lead to a conflict using AVAssetWriter while playing audio with AVAudioPlayer or, really, any audio system. I tried with OpenAL as well.

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.

  • This only happens on the device, not the sim.

  • The error occurs when attempting to create a pixel buffer from CVPixelBufferPoolCreatePixelBuffer.

  • Once the audio starts playing, the AVAssetWriterInputPixelBufferAdaptor.pixelBufferPool which existed before suddely becomes nil.

You can download the representative project here: http://www.mediafire.com/?5k7kqyvtbfdgdgv

Comment out AVAudioPlayer play and it will work on the device.

Any clues are appreciated.


Solution

  • 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'.

    You can use a category that is mixable like AVAudioSessionCategoryAmbient.

    However, I needed to use 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
                       );