AVAudioPlayer no reproduce la grabación realizada con AVAudioRecorder

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

preguntado el 28 de agosto de 12 a las 11:08

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... -

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. -

Hmmm, then ensure your system volume is not muted! XO ... -

2 Respuestas

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];

Respondido 28 ago 12, 11:08

2 channels are for stereo, 1 for mono...don't think AVNumberOfChannelsKey debe be 1! - tipycalFlow

Yes but microphones only record in mono anyway. - Wasim

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 en blanco. Esto no sucede cuando AVNumberOfChannelsKey es 1. - tipycalFlow

Try this link's code. In this you can record an audio and retrieve from document folder.

Grabe un archivo de audio y guárdelo localmente en iPhone

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.