Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:52:26+00:00 2026-05-25T23:52:26+00:00

I am using an AVCaptureSession to capture audio and video samples from the devices

  • 0

I am using an AVCaptureSession to capture audio and video samples from the devices microphone and camera.

I am then attempting to write the CMSampleBuffers (Using AVAssetWriter and AVAssetWriterInputs) returned via the AVCaptureSessions delegate method

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:    (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

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

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}

Here is the code I use to create my AVAssetWriter and AVAssetWriterInputs

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:&error];
}
else
{
    m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeQuickTimeMovie error:&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(&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: &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: &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];

Does anyone know how to correctly write audio samples returned from AVCaptureSession using an AVAssetWriterInput configured to write AAC?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:52:27+00:00Added an answer on May 25, 2026 at 11:52 pm

    I managed to get it to work by changing the AVEncoderBitRateKey parameter passed as the desired output settings from 96 to 64000.

    My audio settings for initializing an AVAssetWriterInput capable of writing AAC audio now looks like

        NSDictionary*  audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                        [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
                                        [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                                        [ NSData dataWithBytes: &acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
                                        [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                                        nil]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was able to capture video frames from the camera using AVCaptureSession according to
I'm using AVAssetWriter with a AVAssetWriterInputPixelBufferAdaptor to capture video from an AVCaptureSession. I'm writing
I'm using the AVFoundation classes to capture the live video stream from the camera
I've been trying to write a video+audio using AVAssetWriter and AVAssetWriterInputs. I read multiple
I downloaded the latest video capture samples from WWDC 2010 and am trying to
Here's the problem. I am using AVCaptureVideoDataOutput to get video frames from camera and
My iphone application captures the realtime data from camera using AVFoundation's AVCaptureSession. I'm able
Hey there, I am trying to access raw data from iphone camera using AVCaptureSession.
I am attempting to grab frames and preview the video from a Bodelin Proscope
I want to be able to (live) stream the frames/video FROM the iPhone camera

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.