Swift - discover music, whether it's Spotify or iTunes

I am currently using the following instruction to discover music:

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

Great, but it will only work for the iTunes player, and not for music that may appear from another application, in particular, it talks about Spotify .

I don’t need to know what song is playing, is it just that something is playing, so I can decide if I provide my own background music for my game or not.

Edit: Ideally, the solution should cover any third-party music program, not just Spotify.

+5
source share
1 answer

Provided by iOS: How to determine if music plays in any background music app?

Swift version:

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

However, the docs developers suggest that instead of iOS 8.0, secondaryAudioShouldBeSilencedHint should be used instead:

 if (AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint()) { print("another application with a non-mixable audio session is playing audio") } 
+10
source

All Articles