Play some sound in background, some sound only in foreground

89 Views Asked by At

I want to play sound in background for a feature but I want to play sound only in foreground for another feature. Also, I want to play sound even when the silent switch is turned on.

To play sound in background, the following 2 code is needed:

  • [Info.plist] UIBackgroundModes = audio

  • AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])

To play sound only in foreground, I think I have to set UIBackgroundModes to empty in the Info.plist. But it is not possible while the app is running.

How can I play sound only in foreground for a feature and sound in background for another feature?

* playing sound in foreground means that sound is paused when the user push the home button of the iPhone and it is resumed when the user open the app again.

1

There are 1 best solutions below

0
On

How can I play sound only in foreground for a feature and sound in background for another feature

Detect when the app goes into the background, and stop playing the foreground sound and switch to the background sound.