iOS - AVAudioSession Route Change Notification not work as expected - ios

I have a app that can play sound. it works fine in usually. However when I Repeatedly disconnect and connect the Bluetooth headset, iPhone status bar shows the Bluetooth headset has been connected, and sound play is not in Bluetooth output route(maybe built-in receiver or speaker).
In my code, the last few disconnect and connect have not been notified from AVAudioSession Route Change Notification. And I get current output route from [[[[AVAudioSession sharedInstance] currentRoute] outputs] firstObject] is also not the bluetooth output.
Is this an issue of iOS? and Can I handle it with code? cause I want to play sound in a Bluetooth route in above case.
Thanks!!!

Related

respond to bluetooth headsets play/pause events

I've been able to respond to wired headphone events (play/pause) using MPRemoteCommandCenter, however this doesn't work for airpods.
Anything different I need to do to observe airpods or bluetooth headphones play/pause/button events? Thanks!

Why don't hear music after recording in swift?

I'm making a karaoke application. If the headphones aren't plugged in, my app works fine. (my voice and background music record together). It is successful, but when I do the same way with headphone then I listen to the recording. I can't hear the background music, but I hear the voice clearly. I attached the code below I used:
https://github.com/genedelisa/AVFoundationRecorder
The issue is that when the headphones are plugged in, the mic is not able to pick up and record the audio, only your voice. Obviously, when the headphones are not plugged in, this is not an issue.
I would recommend that you combine the music and the voice recording after the voice is recorded if the user was using headphones. Suggestions on how to do that can be found on this post: How to merge two mp3 files iOS?
Also, to check if headphones are plugged in check out this post: Are headphones plugged in? iOS7

iOS Force Audio Output only to headset jack

I want to direct iOS VoiceOver sound to headphone even if it's not plugged in.
In other words, while my app is open, VoiceOver sound (and other sounds played by me in app using AVSpeechUtterance etc.) should NEVER go to speakers, but should come out of headset, if connected.
Can anyone suggest something on this?
iOS devices will not power-up the headset jack for audio output unless a recognized (proper impedance, etc.) headset or headphone is currently plugged in to the jack.
VoiceOver audio will always go to the headset, if a valid one is plugged-in, and not over-ridden.
Apparently it's not possible to forcefully direct sound to headphone unless an accessory is plugged to headphone jack (which activates a physical switch to direct voice to headphone).
I've achieved my purpose using following code (in Swift) which directs VoiceOver and other sounds to phone speaker (from where we listen to phone calls) and silences loud speaker while my app is in foreground.
let session: AVAudioSession = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.None)
try session.setActive(true)
} catch {
print("Couldn't override output audio port")
}

issue while vibrating iPhone ios sdk

I want to vibrate and flash light simultaneously.But when I Use following code i only get flash light.
if (some condition)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self setTorch:YES];
}
But when i use only
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
my phone vibrates.
I am unable to find the issue.
May be it will useful for you
find this Gonna Vibrate the device to alert the user? Read this first
Both the functions vibrate the iPhone. But when you use the first function on devices that don’t support vibration, it plays a beep sound. The second function on the other hand does nothing on unsupported devices. So if you are going to vibrate the device continuously, as a alert, common sense says, use function 2.
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

How can I prevent my iOS VoIP app from playing audio when the user switches their phone to silent?

I'm building a VoIP app on iOS and I'm using the AVAudioSession category of AVAudioSessionCategoryPlayAndRecord, which is recommended for VoIP apps that need to constantly play and record audio.
However, when the user switches their iPhone ringer to silent mode, the VoIP application will still play sound for an incoming call. This is not desired behavior.
Is there a way to prevent the incoming calls from playing audio when the user has their phone on silent, but still allow them to answer the call and have audio resume?
If you take a look here at the docs for AVAudioSession Categories, you can see that AVAudioSessionCategoryPlayAndRecord unfortunately does not obey the silencing of a phone, as seen below:
Your audio continues with the Silent switch set to silent and with the screen locked. (The switch is called the Ring/Silent switch on iPhone.)
If you want the audio to stop when you turn the phone to silent, you should use AVAudioSessionCategoryAmbient.
I think I found a solution that works for a VoIP application. If the phone is set to silent, I wanted the phone to show an alert and vibrate instead of playing the audible ring.
I can solve this by using local notifications and moving the sound out of the application and adding it to the notification itself. By specifying a sound file as part of the notification, iOS will handle whether it should play the audio or vibrate the phone. This is determined by the position of the Ring/Silent switch on the side of the phone.
Here's an Apple article on adding a sound to a local notification.

Resources