AVAssetWriter not recording audio

Im having trouble getting audio recorded into a video using avassetwriter on the iPhone. I am able to record video from the camera on the phone no problem but when I try to add audio I get nothing, also the durations displayed in the video in the photo albums app are showing something really out of whack, a 4 second video will show 15:54:01 or something like that, and every video made after the number increases even if the video is shorter. I've been trying to follow what ive seen in other questions here but no luck.

Heres how I'm setting up my audio inputs

captureSession = [[AVCaptureSession alloc] init];
//set up audio input 
AVCaptureDevice *audioDevice     = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio];
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error ]; 
audioOutput = [[AVCaptureAudioDataOutput alloc] init];

if([captureSession canAddOutput:audioOutput])
{
    [captureSession addOutput:audioOutput];
}    

[audioOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

heres how im setting up the AVAssetWriter

videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:MOVIE_PATH] fileType:AVFileTypeQuickTimeMovie error:&error];

AudioChannelLayout acl;
bzero( &acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;


NSDictionary* audioOutputSettings = 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: 64000 ], AVEncoderBitRateKey,
                           nil];

and then here is how im writing the audio sample buffers using the CMSampleBufferRef Sent by the audioOutput call back

- (void) captureAudio:(CMSampleBufferRef)sampleBuffer
 {
   if([audioWriterInput isReadyForMoreMediaData]){
     [audioWriterInput appendSampleBuffer:sampleBuffer];
   }
 }

Would really appreciate any help, I've been stuck on this all day.


I don't see you calling [videoWriter startSessionAtSourceTime] , also you're discarding audio sample buffers when audioWriterInput isn't ready (and that can't be what you want).

So your problem lies in the PTSs (Presentation Time Stamps) of what you're writing out. Either you can tell the output that your timeline starts at a given time t with startSessionAtSourceTime or you can modify the buffers you append to have zero based presentationTimeStamps.

链接地址: http://www.djcxy.com/p/72042.html

上一篇: 使用AVAssetWriter时,PixelBuffer不会被释放

下一篇: AVAssetWriter不录制音频