3
大家好! 我正在處理一個應用程序。它可以錄製音頻文件和郵件給其他人。音頻文件保存爲(.aac)文件。然後我想繼續錄製已保存爲.aac文件的文件。所以我使用AVAssetReader和AVAssetWriter來製作它,但我失敗了!以下是我的代碼:如何使用AVAssetReader和AVAssetWriter創建AAC文件?
AVMutableComposition *mixComposition = [AVMutableComposition composition];
NSURL *url = [NSURL fileURLWithPath:[DOCUMENTS_FOLDER stringByAppendingPathComponent:buttonTitle]];
NSURL *anotherUrl = [NSURL fileURLWithPath:[DOCUMENTS_FOLDER stringByAppendingPathComponent:[fileList objectAtIndex:0]]];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset *preAudioAsset = [[AVURLAsset alloc]initWithURL:url options:nil];
CMTimeRange preTimeRange = CMTimeRangeMake(kCMTimeZero, preAudioAsset.duration);
AVMutableCompositionTrack *preCompositonTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[preCompositonTrack insertTimeRange:preTimeRange ofTrack:[[preAudioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:nextClipStartTime error:nil];
nextClipStartTime = CMTimeAdd(nextClipStartTime, preAudioAsset.duration);
AVURLAsset *cruAudioAsset = [[AVURLAsset alloc]initWithURL:anotherUrl options:nil];
CMTimeRange cruTimeRange = CMTimeRangeMake(kCMTimeZero, cruAudioAsset.duration);
[preCompositonTrack insertTimeRange:cruTimeRange ofTrack:[[cruAudioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:nextClipStartTime error:nil];
//AVAssetReader part
NSError *error;
AVAssetReader *assetReader = [[AVAssetReader assetReaderWithAsset:mixComposition error:&error]retain];
if(error)
{
NSLog(@"error:%@",error);
}
AVAssetTrack *audioTrack = [[mixComposition.tracks objectAtIndex:0]retain];
AVAssetReaderOutput *assetReaderOutput =[[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:nil]retain];
if(![assetReader canAddOutput:assetReaderOutput])
{
NSLog(@"Can't add output!");
return;
}
[assetReader addOutput:assetReaderOutput];
NSURL *exportUrl = [NSURL fileURLWithPath:[DOCUMENTS_FOLDER stringByAppendingPathComponent:@"combine.aac"]];
//AVAssetWriter part
AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportUrl fileType:AVFileTypeMPEG4 error:&error]retain];
if(error)
{
NSLog(@"error:%@",error);
return;
}
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
[ NSData dataWithBytes: &channelLayout length: sizeof(AudioChannelLayout) ], AVChannelLayoutKey,
[ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
nil];
AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:outputSettings]retain];
if([assetWriter canAddInput:assetWriterInput])
{
[assetWriter addInput:assetWriterInput];
}
else
{
NSLog(@"can't add asset writer input .....");
return;
}
assetWriterInput.expectsMediaDataInRealTime = NO;
[assetWriter startWriting];
[assetReader startReading];
CMTime startTime = CMTimeMake(0, audioTrack.naturalTimeScale);
[assetWriter startSessionAtSourceTime:startTime];
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue",NULL);
[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^ {
while(assetWriterInput.readyForMoreMediaData)
{
CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
if(nextBuffer)
{
//append buffer
[assetWriterInput appendSampleBuffer:nextBuffer];
}
else
{
[assetWriterInput markAsFinished];
[assetWriter finishWriting];
[assetReader cancelReading];
[assetReader release];
[assetReaderOutput release];
[assetWriter release];
[assetWriterInput release];
break;
}
}
} ];
我只是想記錄另一個文件中,然後後來追加到以前的,但有一個錯誤: - [AVAssetWriterInput appendSampleBuffer:]輸入緩衝區必須是線性PCM格式時outputSettings不是零'
我想問我採取的方法是正確的嗎?如果我將它製作成PCM格式(.wav),它的通道很好,但是錄製文件的大小太大了。如何解決所提到的錯誤。我如何通過代碼將.wav轉換爲.aac文件?幫助!
你找到一個解決的辦法? – Baza207 2012-08-28 23:13:03