Frage

I use audio recorder to record and create file in the document directory, then I play it using audio player. I am able to record it on both simulator and device, but I can't play it on device only.

Notice that 1. I don't play sound from files, I play from NSData (I store music into database, and retrieve them to play) 2. It works fine on simulator but failed on device.

Here is my code for playing audio

- (void)playAudioWithData:(NSData *)data {

    [self stopPlayingAudio];
    [self stopRecordingAudio];


    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [audioSession setActive:YES error:nil];


    self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];

    [self.audioPlayer play];

}

this is the setting for recording:

    NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithFloat:44100],AVSampleRateKey,
                                   [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
                                   [NSNumber numberWithInt: 1],AVNumberOfChannelsKey,
                                   [NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,nil];
War es hilfreich?

Lösung

Instead of using AVAudioSessionCategoryPlayAndRecord try setting the audio session to AVAudioSessionCategoryRecord when recording and then to AVAudioSessionCategoryPlayback when you want to play back what you just recorded.

I experienced the same thing in my app when recording and playing back audio and this resolved the issue.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top