AVAssetWriter Woes

前端 未结 2 1728
耶瑟儿~
耶瑟儿~ 2020-12-28 20:19

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

相关标签:
2条回答
  • 2020-12-28 20:50

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

    0 讨论(0)
  • 2020-12-28 20:57

    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
                       );
    
    0 讨论(0)
提交回复
热议问题