Reputation: 5113
This may be a really simple problem but I've made a recording using AVAudioRecorder
, then I stopped the recorder. After stopping the AVAudioRecorder
, I press another button to play the recording but it doesn't play. The file exists, I can play it on my computer, even the code knows it exists, there is no error, but refuses to play. What can be the issue?
NSError *error = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:[self.recorder.url path]]) {
self.recordingPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recorder.url error:&error];
self.recordingPlayer.delegate = self;
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
} else {
[self.recordingPlayer play];
}
} else {
NSLog(@"Recording file doesn't exist");
}
EDIT: just tried it on my device and it works fine, plays the recording. It just doesn't work on iOS simulator
Upvotes: 1
Views: 1941
Reputation: 942
Try this link's code. In this you can record an audio and retrieve from document folder.
Record audio file and save locally on iPhone
Upvotes: 0
Reputation: 5113
The problem was with my record settings, I had the number of AVNumberOfChannelsKey
set to 2, for some reason iOS simulator didn't like this, but iPhone was fine with it. Either way, my recording shouldn't have 2 channels in the first place, so good thing I spotted this.
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
nil];
Upvotes: 1