Hello All,
I have been working on a audio application where a user records the audio and later I have to give user the facility to edit that recorded audio as well. The audio being recorded is in .caf format.. herez the code for the setting em using to record the audio
Code:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
[audioSession setActive:YES error:nil];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleeIMA4 ] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:128000] forKey:AVEncoderBitRateKey ];
[recordSetting setValue :[NSNumber numberWithInt:AVAudioQualityMax] forKey:AVEncoderAudioQualityKey ];
I have also developed a function where user can trim an existing audio file by giving the start and end time to create a new audio file. Everything is working fine and audio is being trimmed But after trimming my device/simulator cannot read the duration of the new audio file and displays like -3294323423.9000 etc. Here is the code I am using to trim the audio
Code:
-(IBAction) cutButtonTapped: (id) sender {
// inSeconds and outSeconds are float variables which value is being set using the UISlider
if ((inSeconds > 0) && (outSeconds > 0)) {
if (inSeconds > outSeconds) {
NSLog(@"inSeconds should not be greater than outSeconds";
return;
}
CMTime inTime = CMTimeMakeWithSeconds(inSeconds, 600);
CMTime outTime = CMTimeMakeWithSeconds(outSeconds, 600);
CMTime duration = CMTimeSubtract(outTime, inTime);
CMTimeRange editRange = CMTimeRangeMake(inTime, duration);
NSError *editError = nil;
// Composition is an AVMutableComposition Object which is already initialized
[composition insertTimeRange:editRange
ofAsset:sourceAsset
atTime:composition.duration
error:&editError];
if (!editError) {
timeSlider.maximumValue =
CMTimeGetSeconds (composition.duration);
} else {
NSLog (@"edit error: %@", editError);
}
// reset edit points
inSeconds = -1;
outSeconds = -1;
inLabel.text = @"-:--:--";
outLabel.text = @"-:--:--";
[self exportAudio];
}
}
ANd finnally exporting the audio file to url path.
Code:
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition
presetName:AVAssetExportPresetMediumQuality];
NSLog (@"can export: %@", exportSession.supportedFileTypes);
NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
NSString *exportPath = [documentsDirectoryPath stringByAppendingPathComponent:@"newAudio.caf"];
NSLog(@"Doc Path: %@", exportPath);
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = @"com.apple.m4a-audio";
NSLog(@"File Types: %@" , exportSession.supportedFileTypes);
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSLog (@"status is %d",
exportSession.status);
switch (exportSession.status) {
case AVAssetExportSessionStatusFailed:
case AVAssetExportSessionStatusCompleted: {
[self performSelectorOnMainThread:@selector (doPostExportUICleanup:)
withObject:nil
waitUntilDone:NO];
break;
}
};
}];
exportProgressView.progress = 0.0;
exportProgressView.hidden = NO;
exportTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector (updateExportProgress:)
userInfo:exportSession repeats:YES];
}
Please tell me where I am doing it wrong. Any help will be greatly appreciated and to improve the code any suggestion will also be highly encouraged. I have also posted almost the whole code so that it might help someone else who is looking for the same solution.