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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With