I am trying to make a voice recorder which behaves as following
- Record voice
- Pause the voice
- Listen to the recording
- Continue Recording
I tried doing this:
Initialize the Recorder
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *dataPath = [cacheDir stringByAppendingPathComponent:@"/AudioFiles"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
NSError *error=nil;
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSArray *pathComponents = [NSArray arrayWithObjects:
dataPath,
[NSString stringWithFormat:@"%@.m4a",self.FileName],
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:22050.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithFloat:32000.0] forKey:AVEncoderBitRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:AVAudioBitRateStrategy_Variable forKey:AVEncoderBitRateStrategyKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
Record
[recorder record];
Pause the record
[recorder pause];
Play the record
playingSession = [AVAudioSession sharedInstance];
[playingSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[playingSession setActive: YES error:nil];
NSError *error = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:&error];
NSLog(@"Error initializing player: %@", error);
[player setDelegate:self];
[player play];
So I get the following error
Error Domain=NSOSStatusErrorDomain Code=1685348671 "(null)"
Any idea how can I make this work?