iOs SDK. Shared audio session while playin background audio - ios

I'm faced with very interesting problem and now I'm trying to understand how to sort it out. In fact, my application plays sound when it come into background. Everything is ok, but when I run application like youtube it make interuption for my audio session. I use audio session delegate methods to catch this moment. My question is that how to restore my audio session in background after I kill youtube?
I've tried this:
NSError *err = noErr;
[[AVAudioSession sharedInstance] setActive: YES error: &err];
if(err != noErr)
{
NSLog([err description]);
}
but it doesn't work.
Any suggestion?
Thank you.

Ok, as I understood, to make audio session mixed with other application I have to set special property like this:
UInt32 allowMixing = true;
AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers,
sizeof (allowMixing),
&allowMixing
);
And now I can see that my application plays audio in background even if I start youtube. The problem is that it plays audio simultaneously :(. I can hear my application's audio and youtube as well. There is not audio session interruption. Strange problem.

Related

AVAudioSession mix with WKWebview

I am working on a VoIP app. For that I had set the category of AVAudioSession to AVAudioSessionCategoryPlayAndRecord.
All the app functionality was working fine till now.
Then we had a new requirement where within app when the voice call is going on, user can play one video embedded in WKWebView.
Now when user plays video from WKWebView, video plays successfully and the volume of video is also as expected. But when user stops/pauses the video then voice call gets disconnected.
So I came to know that WKWebView runs in different process than the app, so in order to make my app's audio mixable with WKWebview I have to set the AVAudioSession as mixwithOthers.
I have done that with the following code...
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *errorInSettingCategory;
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions: AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionAllowBluetooth error:&errorInSettingCategory];
Now after playing video form WKWebView I am able continue with my voice call.
But this approach introduced the new bug.
As now the AVAudioSession is mixable, volume of the video from WKWebView is quite low and app's audio (voice call audio) is quite dominant.
I have tried different setcategory options but with no luck.
I want to have volume of app's audio and volume of WKWebView's video at same level.
Thank you for any help.
Try setting the duckOthers property of your AVAudioSession to false:
https://developer.apple.com/documentation/avfoundation/avaudiosession/categoryoptions/1616618-duckothers

YouTube volume is very low when played during a WebRTC session

I am working on an iOS app with WebRTC. During the chat, volume is fine. But if I play an embedded youtube video through WKYTPlayerView, volume of the video is very low. If I disconnect the chat, video volume becomes normal again.
I have tried several things and I have been looking at this issue for few days. Here is how I am setting the category for audio session;
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions: AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
error: &error];
if (error != nil) {
NSLog(#"Problem setting AVAudioSession to mix with others");
}
Any help is very much appreciated.
AFAIK, there are raised WebRTC issues. Here's 2 of them:
Low sound volume during calls
AGC sets volume to zero
You may want to check the given fix which is putting up a CL to fix the problem in all platforms. See if it also works for you.
Otherwise, you can try using mediastream-gain which is useful for controlling rhe volume of your microphone input before it's sent accross a peer connection in a WebRTC call.
Lastly, you may want to also check this thread for additional insights on application sharing to play YouTube videos.

iPhone: Play sound when in Silent mode iOS 10

I've read many posts and articles and am having no luck playing audio when iPhone is in silent. I'm using react-native-sound which uses AVAudioPlayer under the hood. The following calls succeed without any luck with sound coming through when in silent.
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }
Any suggestions?
Resources:
https://developer.apple.com/library/content/qa/qa1668/_index.html
I am struggling with this as well. I don't think it is totally possible. I can have sound play in silent mode when the app is in foreground then continue playing while it is in the background, but I don't think iOS allows you to trigger a sound to play while the app is in the background and in silent mode. I am using local notifications to trigger the sound to play.
Some people have had luck setting 'Application does not run in background' to YES in the info.plist which keeps the app in the foreground when someone locks there phone with the app still in the foreground but I need the app to work if the phone is locked when the app is in the background.
Currently the only solutions I know of are to play a silent audio track then tell it to keep it active after playing (to not waste cpu/battery). See link here. Apple normally doesn't approve of this in app review. Or to use location updates.

How to force play audio on iPhone speaker?

I'm developing a mobile application to record audio. Functionality as follows. There is one static audio file on my application (which plays drum sound). When user start recording he can play this drum sound and start speaking/singing. Here I'm facing a problem with the static sound, when the recording is done and the user play back the same recorded audio file the static audio file sound (Drum sound in my case) is not audible properly like the way how user voice audible. If I can route the static audio file sound to the iPhone speaker even ear phone plugged in that will solve the problem. Can any one please help me how can I force play audio through speaker even though ear phone plugged in ? Thank you so much,
You can try this, it solves my problem.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
Try this,
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
} catch _ {
}

iOS 7+ AVAudioPlayer from phone's Internal Ear Piece

I'm trying to figure how i can play audio using AVAudioPlayer to the phone's internal ear piece.
When headphones are connected the audio should play on those else it must play from internal ear piece (AVAudioPlayer should never play from Phone's Bottom Speaker).
Is there any way to achieve this? iOS 7+ answers are welcome!
It's interesting to see how many more answers there are regarding Android questions, iOS questions are much less answered.
This is the solution that i managed to find:
NSError *error;
// Initialize Audio Player
_player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:_audioPath] error:&error];
// Set Audio Session to "Play and Record", UNFORTUNATELY this is the only way to play
// from the internal speaker. It asks the user to grant Recording permission to the app.
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if (setCategoryError) {
CLS_LOG(#"Error routing audio: %#",setCategoryError);
[self.navigationController popViewControllerAnimated:YES];
}
[_player prepareToPlay];
// And now you can play from the internal speaker.
You can see that i had to use AVAudioSessionCategoryPlayAndRecord because it's the only category that routes the audio to the receiver (the small speaker you hold to your ear when on a phone call) see the audio session programming guide for reference.
Actually, there is a method named overrideOutputAudioPort:error: that looks promising but it can only be used AVAudioSessionCategoryPlayAndRecord to force the device to use the external speaker, and not vice versa.

Resources