6

I am currently using the following statement to detect music:

if MPMusicPlayerController.systemMusicPlayer().playbackState == .Playing {
    print("There is music playing")
}

Great, however this will only work for iTunes player, and not music that might be coming from a different app, specifically talking about Spotify.

I don't need to know the song being played, simply whether there is anything playing at all, so I can decide whether I provide my own background music for my game or not.

Edit: ideally the solution should cover any 3rd party music program, not just Spotify.

zantuja
  • 211
  • 1
  • 4
  • 14

1 Answers1

13

Given iOS: How do I detect if music is playing in any background music app?

the Swift version would be:

let isOtherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying()

However, the developer docs suggest that starting with iOS 8.0 you should use secondaryAudioShouldBeSilencedHint instead:

if (AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint()) {
   print("another application with a non-mixable audio session is playing audio")
}
Community
  • 1
  • 1
szym
  • 5,606
  • 28
  • 34