Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play and record video while playing some background music

Tags:

ios

I looked in several forums and they all couldn't deliver a satisfying answer.

My wish is to be able to play and record video while playing music at the background. I managed to do that with the help of a snippet I found. here is the code:

    AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;

NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

OSStatus propertySetError = 0;

UInt32 allowMixing = true; 

propertySetError = AudioSessionSetProperty (

                                            kAudioSessionProperty_OverrideCategoryMixWithOthers,  // 1

                                            sizeof (allowMixing),                                 // 2

                                            &allowMixing                                          // 3

                                            );
[session setActive:YES error:&error];

The problem is while recording, I can only hear the background music through the ear speaker instead of the regular speaker.

How can I set the regular speaker to work so that the recording session won't be interrupted?

like image 872
Amit Avatar asked Dec 11 '11 10:12

Amit


People also ask

Can you film a video while playing music?

Launch your music app and play the track. Open the Record Video with Music app and tap the camera icon to start recording. Tap the same camera icon to stop the recording. The app works smoothly with any music app such as Spotify, Gaana, YouTube Music and Apple Music.


1 Answers

When you override the seesion category with *kAudioSessionProperty_OverrideCategoryMixWithOthers* it uses the smaller speaker on the top side of the device (besides the frontend camera). To use the regular speaker as the output device, you should instead redirect the output route of your audio session. The following is the code snippet of achieving this

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

AudioSessionSetProperty ( 
    kAudioSessionProperty_OverrideAudioRoute,                         
    sizeof (audioRouteOverride),                                      
    &audioRouteOverride                                               
); 

Replace this code with your above and it will work. You can further refer Redirect Output Audio on apple developer portal.

like image 55
Yiqun Hu Avatar answered Sep 22 '22 12:09

Yiqun Hu