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 _ {
}
Related
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
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.
We are developing a VOIP application and while there is an active call, we want not to allow other apps play music or something (like Whatsapp does). Is there any way of doing it in a normal way, can you help please?
Edit 1: I think we need to start with listening the observer AVAudioSessionInterruptionNotification.
Accepted Answer Edit 2:
When sound interruption begins, try to only active the AudioSession again. This works. Thank you KudoCC.
Here is a link talks about audio category, there is a table in it:
As your app supports VoIP, so it need to play and record audio, and you need interrupt non-mixable apps audio, so AVAudioSessionCategoryPlayAndRecord is the right category. Also you should re-active your audio session when you're playing/recording audio so that the category takes effect.
Try this code in your app to pause the background audio when calls,
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
It is helpful when other apps uses AVAudioPlayer.
For more detail refer this Link
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.
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.