1

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

Wasim
  • 4,953
  • 10
  • 52
  • 87
  • does it log the error : `NSLog(@"error: %@", [error localizedDescription]);` or does the control go into `[self.recordingPlayer play];`? If it logs the error, add it to your question... – tipycalFlow Aug 28 '12 at 10:13
  • By the way, i've made an edit, works on device but not on simulator. There is no error, it calls the play method. I even have the delegate method `- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error` which isn't called either. – Wasim Aug 28 '12 at 10:15
  • Hmmm, then ensure your system volume is not muted! XO ... – tipycalFlow Aug 28 '12 at 10:16

2 Answers2

1

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];
Wasim
  • 4,953
  • 10
  • 52
  • 87
  • 2 channels are for stereo, 1 for mono...don't think `AVNumberOfChannelsKey` **should** be 1! – tipycalFlow Aug 28 '12 at 11:11
  • Yes but microphones only record in mono anyway. – Wasim Aug 28 '12 at 12:01
  • 1
    Corerct...you'll notice that if `AVNumberOfChannelsKey` is 2 and you play this sound back in a headphone(say), you'll hear the recorded sound on only one side(even though the recorded sound is mono?) because the sound is recorded as stereo with one channel **blank**. This doesn't happen when `AVNumberOfChannelsKey` is 1. – tipycalFlow Aug 28 '12 at 12:24
0

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

Community
  • 1
  • 1
Hardeep Singh
  • 942
  • 8
  • 15