I have a project in which the app records audio files using avrecorder. The requirement is that
I should be able to pause the recording and play the recorded file and then continue recording. I searched and found that, we are not able to play the paused recording and then continue the record. So I used the AVExportSession to append the audio file to first file. Now here arises the next problem. When I export the file, it only allows us to export to AVAssetExportPresetAppleM4A (M4a). I need the files to be in smaller size for uploading it to the server. The exported file is of large size. I am already recording the assets in very low quality and low sampling rate. The user may either send the first file or the appended file over the internet. Is there any solution for this problem.
- (BOOL)combineFilesFor:(NSURL*)url
{
NSError *error = nil;
BOOL ok = NO;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *soundOneNew = [documentsDirectory stringByAppendingPathComponent:Kcombined];
CMTime nextClipStartTime = kCMTimeZero;
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:masterUrl options:nil];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
if ([tracks count] == 0)
return NO;
CMTimeRange timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [avAsset duration]);
AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
ok = [compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:&error];
if (!ok) {
NSLog(@"Current Video Track Error: %@",error);
}
nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration);
AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks1 = [avAsset1 tracksWithMediaType:AVMediaTypeAudio];
if ([tracks1 count] == 0)
return NO;
CMTimeRange timeRangeInAsset1 = CMTimeRangeMake(kCMTimeZero, [avAsset1 duration]);
AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
ok = [compositionAudioTrack insertTimeRange:timeRangeInAsset1 ofTrack:clipAudioTrack1 atTime:nextClipStartTime error:&error];
if (!ok)
{
NSLog(@"Current Video Track Error: %@",error);
}
AVAssetExportSession *exportSession = [AVAssetExportSession
exportSessionWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
if (nil == exportSession)
return NO;
exportSession.outputURL = [NSURL fileURLWithPath:soundOneNew]; // output path
combinedUrl = [NSURL fileURLWithPath:soundOneNew];
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"AVAssetExportSessionStatusCompleted");
} else if (AVAssetExportSessionStatusFailed == exportSession.status) {
NSLog(@"AVAssetExportSessionStatusFailed :%@",exportSession.error);
} else {
NSLog(@"Export Session Status: %d", exportSession.status);
}
}];
return YES;
}
Aucun commentaire:
Enregistrer un commentaire