I\'m trying to use AVAssetWriter to write CGImages to a file to create a video from images.
I\'ve gotten this to work successfully in three different ways on the sim
Well, first you need to pass some bufferAttributes
when creating that adaptor object:
NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, nil];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:_videoWriterInput
sourcePixelBufferAttributes:bufferAttributes];
Then remove that call to CVPixelBufferPoolCreate
, there's already a pixel buffer pool created in the adaptor object, so call just this instead:
CVPixelBufferRef pixelBuffer = NULL;
CVPixelBufferPoolCreatePixelBuffer(NULL, adaptor.pixelBufferPool, &pixelBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
// ...fill the pixelbuffer here
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CMTime frameTime = CMTimeMake(frameCount,(int32_t) 30);
BOOL res = [adaptor appendPixelBuffer:pixelBuffer withPresentationTime:frameTime];
CVPixelBufferRelease(pixelBuffer);
CFRelease(sampleBuffer);
I think that should do it, I've had a similar error at some point and I solved it by creating the adaptor and pixel buffer as shown here..
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
);