Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio will make AVCaptureSession terminate

I wrote a app to record video from iPhone. It work fine but have one big problem. When AVCaptureSession start running and user try to play audio from thier library(iPod). This action will make AVCaptureSession terminate. Have any idea can prevent user try to play audio or solve this problem?


this is my code:

videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];           
audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

AVCaptureDeviceInput *videoDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoDevice error:nil];
AVCaptureDeviceInput *audioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

captureSession = [[AVCaptureSession alloc] init];

[captureSession beginConfiguration];
[captureSession setSessionPreset:AVCaptureSessionPresetHigh];
[captureSession addInput:videoDeviceInput];
[captureSession addInput:audioDeviceInput];
[captureSession addOutput:movieFileOutput];
[captureSession commitConfiguration];

[captureSession startRunning];
like image 929
anistar Avatar asked Nov 14 '22 04:11

anistar


1 Answers

This worked for me:

- (void)setupAudio {
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    UInt32 doSetProperty = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

}

like image 105
zerodog Avatar answered Dec 28 '22 07:12

zerodog