Question

I can't record audio after playing a video, if I record before playing the video no problem but then I have to close the application to record again. I can't record audio after playing a video, if I record before playing the video no problem but then I have to close the application to record again.

Record Audio:

- (IBAction)recordAudio:(id)sender {

dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary
                                dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16],
                                AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2],
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0],
                                AVSampleRateKey,
                                nil];

NSError *error = nil;

_audioRecorder = [[AVAudioRecorder alloc]
                  initWithURL:soundFileURL
                  settings:recordSettings
                  error:&error];

if (error)
{
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [_audioRecorder prepareToRecord];
}

if (!_audioRecorder.recording)
{
    _playButton.enabled = NO;
    _stopButton.enabled = YES;
    [_audioRecorder record];

}
}
Was it helpful?

Solution

Do this before recording the Audio:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top