Play audio in background and mute with mute switch - ios

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.

Related

Background music continue to playing when open an app or change scene

I have game made with SpriteKit. When I open the app and I change the scene, while music is playing (for example from Music app on iPhone), the music stops. How can I continue to play music when I open the app and when I change scenes?
You need to configure your app's capabilities, and set the category on the Audio Session.
I think you're looking for:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
There are other categories as well, but here are the docs on AVAudioSessionCategoryPlayback.
When using this category, your app audio continues with the Silent
switch set to silent or when the screen locks. (The switch is called
the Ring/Silent switch on iPhone.) To continue playing audio when your
app transitions to the background (for example, when the screen
locks), add the audio value to the UIBackgroundModes key in your
information property list file.
Here's a capture of the XCode Capabilities Panel:

AVCaptureSession startRunning is unmuting device

My application has various sound effects for buttons and other actions, if the device is muted/silenced they don't make sounds as expected. However, one of the screens does video recording, and if that screen is navigated to it enables all of the sound effects everywhere in the app. By commenting out some things I determined that it was the startRunning function that does this - I'm not sure if this is just normal behavior because starting the camera enables related things, like audio, or if there's something weird going on that I can change.
If you're doing video recording you're most likely using the AVAudioSessionCategoryPlayAndRecord category. This category will always ignore the mute switch on the side of the device, by design. See here for definitions of all AVAudioSession categories. In short, there's no way to respect the mute switch when using this audio category. So maybe when you switch away from that screen, you should set the audio session category to something else like AVAudioSessionCategoryAmbient if that will not affect your app.

Detect hardware mute button

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

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.

How to start playing a sound while iPod's screen is turned off?

I need to play a custom sound synthesized using AudioUnit on an iPod.
This sound shall be played after up to one hour of playing music from a playlist on the iPod (it is played using MPMusicPlayerController).
The issue is that everything works fine if the screen in on, however my custom sound doesn't play if the screen is off.
If the sound is already playing when the screen is turned off it keeps playing well. So I assume I am using the correct audio session category.
Could you give me any hint?
According to Apple's developer library:
AVAudioSessionCategoryPlayback or the equivalent
kAudioSessionCategory_MediaPlayback—Use this category for an
application whose audio playback is of primary importance. Your audio
plays even with the screen locked and with the Ring/Silent switch set
to silent.
(Also, check this table.)
It seems that setting the audio session category to AVAudioSessionCategoryPlayback or kAudioSessionCategory_MediaPlayback would automatically fix your problem.

Resources