Freezing the camera when opening the application during a call

I created a camera using AVCaptureSession . I configured this for photo and video modes .

The camera and application are working fine. In addition, I enabled background music (if the user is playing a song using the Music App on the iPhone) while the camera is open or video is being recorded. It also works great. (Attached Image 2)

I enabled background music playback with this code

AVAudioSession *session1 = [AVAudioSession sharedInstance]; [session1 setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionAllowBluetooth error:nil]; [session1 setActive:YES error:nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

Now, if I get a call , hide the phone call screen by pressing the "Home" button and open the application, and you want to open the camera screen to capture an image / record video, it opens, but it freezes with an image (Attached Image (1)).

Now my requirement: I want to capture an image / video recording during a phone call. I searched for other apps, and Snapchat does the same , and I can record videos while I call.

Please help me how can I achieve this. enter image description here enter image description here

+7
ios objective-c avcapturesession avaudiosession
source share
1 answer

You need to use the AVCaptureSessionWasInterruptedNotification and AVCaptureSessionInterruptionEndedNotification and disable audio capture during session termination:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionWasInterrupted:) name:AVCaptureSessionWasInterruptedNotification object:self.session]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionInterruptionEnded:) name:AVCaptureSessionInterruptionEndedNotification object:self.session]; // note that self.session is an AVCaptureSession 

-

 - (void)sessionWasInterrupted:(NSNotification *)notification { NSLog(@"session was interrupted"); AVCaptureDevice *device = [[self audioInput] device]; if ([device hasMediaType:AVMediaTypeAudio]) { [[self session] removeInput:[self audioInput]]; [self setAudioInput:nil]; } } - (void)sessionInterruptionEnded:(NSNotification *)notification { NSLog(@"session interuption ended"); } // note that [self audioInput] is a getter for an AVCaptureDeviceInput 

This allows the camera to continue to work and allows you to record still / silent video.

Now about how to connect the sound after the call ends .. let me know if you find out: Call back when the phone call ends? (to resume AVCaptureSession)

+3
source share

All Articles