Detect hardware mute button - ios

How to mute application sound when hardware mute button on iPhone and iPad is on.I want to detect that whenever hardware mute button is turned on and off my application sound will be turned on and off.

You need not listen to the event, just use appropriate audio session category.
AVAudioSessionCategoryAmbient or AVAudioSessionCategorySoloAmbient

Related

How to detect mute button event is triggered in iOS

Neither I don't want to mute or unmutes iPhone nor I want to detect that it muted or unmuted, I just want to know, how can I detect mute button trigger event whether it is on to off or off to on?
There is no native iOS API to detect if the mute switch is enabled/disabled on a device.
The general principle to check if the device is muted is to play a short sound and detect the length it took to play. From this length, we can determine if it was muted or not. Check https://github.com/akramhussein/Mute or https://github.com/moshegottlieb/SoundSwitch

How to use PlayAndRecord session with ability to switch output to Speaker

I'm writing and iOS audio/video chat app with several pre-conditions:
I'm using PlayAndRecord category to establish a audio/video chat.
I have also a button to which allows user to switch to the phone speaker whenever he wants.
I'm using AirPlay to demo the app.
The issue is once I'm switching to speaker during my PlayAndRecord session I'm losing the AirPlay mirroring. Once speaker is disabled - Airplay is back.
How to handle Speakerphone without losing the AirPlay mirroring?

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.

Play audio in background and mute with mute switch

Is it possible to play audio with AVAudioPlayer in the background and also mute it with the mute switch on iPhone?
I know I can play audio in the background if I set AVAudioSessionCategoryPlayback but it will not mute with the mute switch. Or if I set AVAudioSessionCategoryAmbient - it will be muted by a switch but also when pressing home button for example - and I don't want that to happen.
UPDATE:
I have "App plays audio or streams audio.." in "Required background modes" - so when i set AVAudioSessionCategoryPlayback it continues playing in background even after pressing home button but the mute switch has no effect.
No, the desired behaviour is not possible with the available AVAudioSession categories. This might be on purpose to avoid unwanted battery loss or data costs for the users. Users can still decide to lower the volume to zero with the volume buttons of course.
Since there is also no public API anymore to detect the status of the silent/mute switch anymore, workarounds/hacks are tricky and might be declined on App Store submission.

How to make an app to play ringer with behaviors similar to iphone native phone ring

I am working on a VOIP app on iOS. I have encountered some problems when implementing "Ignore incoming ringer" feature. It has the following requirements:
1. Should stop ringer if pressed screen lock or volume button
2. Should stop ringer if mute button is pushed to "mute" state
3. Should not play ringer if mute button is in "mute" state
Since iOS SDK provide no information of the phone's hardware buttons, I can only rely on setting AVAudioSession category. Before playing ringer, I set the category to ambient so screen lock and mute button can stop the audio. But this method cannot satisfy my requirements because app cannot play ringer when in background because ambient category cannot support background audio.
In order to solve this problem, I start playing the ringer in playbackandrecord category to make the audio stream come out and change the category to ambient immediately. Though it can meet my requirements, but this method caused many problems and bugs. For exameple, MPVolumeView cannot display the output devices correctly.
Can anyone provide a more elegant way way to simulate ringer? Or can this be done in iPhone? Since it will be in app store, I cannot use private apis. Thanks in advance.

Resources