AVAudioRecorder won't record IF movie was previously recorded & played

前端 未结 6 1882
无人共我
无人共我 2021-02-02 02:17

My iPhone app uses \"AVAudioRecorder\" to make voice recordings. It also uses \"UIImagePickerController\" to record movies and \"MPMoviePlayerController\" to play movies.

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 02:35

    I was having the same issue. Before I fixed the issue, my recording/playback code was like this:

    Start Recording Function

    - (BOOL) startRecording {   
    @try {
        NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
    [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,  [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey, nil];
    
            NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *soundFilePath = [documentsPath stringByAppendingPathComponent:@"recording.caf"];
    
            if(audioRecorder != nil) {
                [audioRecorder stop];
                [audioRecorder release];
                audioRecorder = nil;
            }
    
            NSError *err = nil;
            audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSetting error:&err];
            [soundFileURL release];
            [recordSetting release];
            if(!audioRecorder || err){
                NSLog(@"recorder initWithURL: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
                return NO;
            }
    
            [audioRecorder peakPowerForChannel:8];
            [audioRecorder updateMeters];
            audioRecorder.meteringEnabled = YES;
            [audioRecorder record];
        }
        @catch (NSException * e) {
            return NO;
        }
    
        recording = YES;
        return YES;
    }
    

    Stop Recording Function

    - (BOOL) stopRecording {
        @try {
            [audioRecorder stop];
            [audioRecorder release];
            audioRecorder = nil;
    
            recording = NO;
        }
        @catch (NSException * e) {
            return NO;
        }
    
        return YES;
    }
    

    Start Playing Function

    - (BOOL) startPlaying {
        @try {
            NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *soundFilePath = [documentsPath stringByAppendingPathComponent:@"recording.caf"];      NSURL * soundFileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
            NSError *err = nil;
    
            if (audioPlayer) {
                [audioPlayer release];
                audioPlayer = nil;
            }
    
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error: &err];
            [soundFileURL release];
            if (!audioPlayer || err) {
                NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
                return NO;
            }
    
            [audioPlayer prepareToPlay];
            [audioPlayer setDelegate: self];
            [audioPlayer play];
    
            playing = YES;
        }
        @catch (NSException * e) {
            return NO;
        }
    
        return YES;
    }
    

    Stop Playing Function

    - (BOOL) stopPlaying {
        @try {
            [audioPlayer stop];
            [audioPlayer release];
            audioPlayer = nil;
    
            playing = NO;
        }
        @catch (NSException * e) {
            return NO;
        }
    
        return YES;
    }
    

    I fixed the recording issue after playing a captured video, the code is as follows:

    - (BOOL) startRecording {   
        @try {
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setCategory:AVAudioSessionCategoryRecord error:nil];
    
            // rest of the recording code is the same .
    }
    
    - (BOOL) stopRecording {
        @try {
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
        // rest of the code is the same
    }
    
    - (BOOL) startPlaying {
        @try {
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
        // rest of the code is the same. 
    }
    
    - (BOOL) stopPlaying {
        // There is no change in this function
    }
    

提交回复
热议问题