Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 9 play audio in silent mode but keep other app's music playback running

Tags:

ios

audio

I have some warning sounds in my app but I really need background music (e.g. Spotify) to keep running in the background. I solved this by

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

However, in silent mode my AVAudioPlayers are muted but the sound is crucial, even in silent mode. I read about AudioServicesPlaySystemSound ignoring silent mode but it does not, at least not on iOS9.

Any other possibilities? Oh and please waste your time by saying things like "I would hate this as a user", "isn't it called SILENT mode", ... this is a programming page, not a philosophy class.

like image 850
user2875404 Avatar asked Aug 29 '15 15:08

user2875404


People also ask

How do you play music in the background on iOS?

Go to Settings > Accessibility > Audio/Visual > Background Sounds, then turn on Background Sounds. Set any of the following: Sound: Choose a sound; the audio file downloads to your iPhone. Volume: Drag the slider.


Video Answer


1 Answers

The Audio Session Category AVAudioSessionCategoryAmbient will obey the Ring/Silent switch as stated in the documentation -

AVAudioSessionCategoryAmbient The category for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off...

... Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

I suggest trying a better suited category with mixing options like -

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
                                 withOptions:AVAudioSessionCategoryOptionMixWithOthers 
                                       error:nil];

AVAudioSessionCategoryPlayback category will ignore the silent switch as mentioned in documentation -

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks.

like image 154
Bamsworld Avatar answered Nov 07 '22 01:11

Bamsworld