0

I have 2 instances of AVAudioPlayer. One for notification sounds and one for music.

var soundPlayer: AVAudioPlayer?
var musicPlayer: AVAudioPlayer?

When the notification sound plays it should be able to .mixWithOthers. This allows the notification to not interrupt another app playing music or video.

try AVAudioSession.sharedInstance().setCategory(.playback, options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)

However, if the user decides to play music via my app, then it should NOT .mixWithOthers as seen here:

try AVAudioSession.sharedInstance().setCategory(.playback)
try AVAudioSession.sharedInstance().setActive(true)

The issue arises that AVAudioSession.sharedInstance() is a singleton and it seems I can only have one setting for my entire app, not per AVAudioPlayer using that method. And that the latest change to .sharedInstance() updates both players.

Is there a way to set different categories setCategory per player AVAudioPlayer?

Rob
  • 6,758
  • 4
  • 46
  • 51

1 Answers1

1

No. As you note, there is only one AVAudioSession. Players do not themselves have a category; it is something your entire app has. You will need to switch the category options when playing music in via your app. You should know when this is happening, so you can set the options appropriately.

Though rare, it is valid to change category options at any time.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610