Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use AVAssetWriter to write AAC audio out in ios?
    primarykey
    data
    text
    <p>I am using an AVCaptureSession to capture audio and video samples from the devices microphone and camera.</p> <p>I am then attempting to write the CMSampleBuffers (Using AVAssetWriter and AVAssetWriterInputs) returned via the AVCaptureSessions delegate method</p> <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer: (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection </code></pre> <p>This works fine when my audio AVAssetWriterInput is configured to write the data out in Apple Lossless format (kAudioFormatAppleLossless) but if i try and configure the audio AVAssetWriterInput to use AAC (kAudioFormatMPEG4AAC) it successfully writes video and audio samples for about 500ms then fails with the following error</p> <pre><code>writer has failed with Error Domain=AVFoundationErrorDomain Code=-11821 "Cannot Decode" UserInfo=0x4b2630 {NSLocalizedFailureReason=The media data could not be decoded. It may be damaged., NSUnderlyingError=0x4ad0f0 "The operation couldn’t be completed. (OSStatus error 560226676.)", NSLocalizedDescription=Cannot Decode} </code></pre> <p>Here is the code I use to create my AVAssetWriter and AVAssetWriterInputs</p> <pre><code>NSError *error = nil; m_VideoCaputurePath = [[NSString stringWithFormat:@"%@/%@.mp4",[UserData getSavePath],[UserData getUniqueFilename]] retain]; if( USE_AAC_AUDIO ) { m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeMPEG4 error:&amp;error]; } else { m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeQuickTimeMovie error:&amp;error]; } //\Configure Video Writer Input NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVideoCodecH264, AVVideoCodecKey, [NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, nil]; m_videoWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain]; m_videoWriterInput.expectsMediaDataInRealTime = YES; //\Configure Audio Writer Input AudioChannelLayout acl; bzero(&amp;acl, sizeof(acl)); acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; NSDictionary* audioOutputSettings; if( USE_AAC_AUDIO ) { audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey, [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey, [ NSData dataWithBytes: &amp;acl length: sizeof( acl ) ], AVChannelLayoutKey, [ NSNumber numberWithInt: 96 ], AVEncoderBitRateKey, nil]; } else { audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys: [ NSNumber numberWithInt: kAudioFormatAppleLossless ], AVFormatIDKey, [ NSNumber numberWithInt: 16 ], AVEncoderBitDepthHintKey, [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey, [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey, [ NSData dataWithBytes: &amp;acl length: sizeof( acl ) ], AVChannelLayoutKey, nil ]; } m_audioWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings] retain]; m_audioWriterInput.expectsMediaDataInRealTime = YES; //\Add inputs to Write NSAssert([m_audioAndVideoWriter canAddInput:m_audioWriterInput], @"Cannot write to this type of audio input" ); NSAssert([m_audioAndVideoWriter canAddInput:m_videoWriterInput], @"Cannot write to this type of video input" ); [m_audioAndVideoWriter addInput:m_videoWriterInput]; [m_audioAndVideoWriter addInput:m_audioWriterInput]; </code></pre> <p>Does anyone know how to correctly write audio samples returned from AVCaptureSession using an AVAssetWriterInput configured to write AAC?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload