12

Accessing the MPMusicPlayerController.systemMusicPlayer() (code below) works for getting track info for what's playing in the Apple Music app, but is there a way we can access information of the current song playing in the Spotify app?

This code posted in this answer I need to know how to get information about which player is currently streaming (player, spotify, napster...) uses MPNowPlayingInfoCenter which is nil whether using Apple Music or Spotify etc.

  let player = MPMusicPlayerController.systemMusicPlayer()

    @IBAction func getMusicButton(_ sender: UIButton) {




        if let mediaItem = player.nowPlayingItem {
            let title: String = mediaItem.value(forProperty: MPMediaItemPropertyTitle) as! String
            let albumTitle: String = mediaItem.value(forProperty: MPMediaItemPropertyAlbumTitle) as! String
            let artist: String = mediaItem.value(forProperty: MPMediaItemPropertyArtist) as! String

            print("\(title) on \(albumTitle) by \(artist)")
        }

    }
Community
  • 1
  • 1
GarySabo
  • 4,076
  • 5
  • 27
  • 76
  • Does this help you? http://stackoverflow.com/questions/33163272/i-need-to-know-how-to-get-information-about-which-player-is-currently-streaming –  Mar 05 '17 at 20:11
  • Possible duplicate of [I need to know how to get information about which player is currently streaming (player, spotify, napster...)](http://stackoverflow.com/questions/33163272/i-need-to-know-how-to-get-information-about-which-player-is-currently-streaming) –  Mar 05 '17 at 20:12
  • No this code doesn't return info for Apple Music or for Spotify, please see my edits above – GarySabo Mar 06 '17 at 15:32
  • Did you find a solution for this? – Kyle Goslan Jul 26 '17 at 18:35
  • I didn't @KyleGoslan my understanding is that it's a sandboxing issue whereas we can't detect what Spotify (a 3rd party app) is playing only with Apple Music is playing. – GarySabo Jul 26 '17 at 18:47
  • @GarySabo, it's odd, I'm not convinced that's the case. Spotify must be broadcasting the info to enable it to play/display info on the iOS media player, so it's odd that we can't then read that info. It appears that that's exactly what 'MPNowPlayingInfoCenter' is supposed to be used for, but returns nil: https://developer.apple.com/documentation/mediaplayer/mpnowplayinginfocenter – Kyle Goslan Jul 26 '17 at 18:49
  • @KyleGoslan that was my experience as well, and so I abandoned my project for now, if you do figure out a solution please repost here – GarySabo Jul 26 '17 at 18:51
  • @GarySabo Will do. Frustrating! So easy to do if it's the music app. – Kyle Goslan Jul 26 '17 at 18:52

1 Answers1

8

The Apple Documentation states there are two types of music player:

  • An application music player plays music locally within your app. It is not aware of the Music app’s now-playing item, nor does it affect the Music app’s state. There are two application music players: applicationMusicPlayer and applicationQueuePlayer. The application queue player provides greater control over the contents of the queue and is the preferred player.
  • The system music player employs the built-in Music app on your behalf. On instantiation, it takes on the current Music app state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The Music app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item.

The first is only within your app and the second is the Apple Music app, since you can't reach out of your app's sandbox in a normal app this is not possible in App Store apps.

If however you still want to create your idea you can develop this on a jailbroken device. This github project has an easy example of how to get the current playing song in a jailbreak tweak. If you want to try it, this is an excellent, be it a bit old, still accurate tutorial on how to get started developing tweaks.


Update

I just ran into this Spotify SDK that will let users login and play the users Spotify music. This way the music stream will be inside your app and you have access to all info in the applicationMusicPlayer. The downside is that this will be a lot more work to set up than just getting the information about Spotify's playing track and the user will have to play his music from your app instead of the Spotify app.

Casper Zandbergen
  • 2,667
  • 20
  • 37
  • I think the sdk is build for something else. As per their description- "This framework contains functionality necessary to stream audio content from Spotify." – Rishabh Aug 02 '17 at 05:53
  • @Rishabh streaming spotify audio content means that the music information will be in your apps applicationMusicPlayer does it not? – Casper Zandbergen Aug 02 '17 at 05:55
  • Oh, I see. Thats probably correct. But i see an open feature request for this same thing at git: https://github.com/spotify/ios-sdk/issues/418 – Rishabh Aug 02 '17 at 05:58
  • I think this answer is incorrect. The `Bose Connect` iOS app successfully does this. It can get currently playing media details and playback controls for `Audible` and `Spotify`. `Bose Connect` does require that the other apps be alreadyplaying for you to control playback and get media details. I am looking at replicating Bose Connect's playback feature. Though I wonder if the bluetooth audio device they connect to, QC35, is receiving the data and sharing it with the app, not systemMusicPlayer. – Ryderpro Mar 06 '19 at 23:38
  • @Ryderpro I assume they do it through the bluetooth but it is possible that the api changed. This answer is a year old, if you find anything don't hesitate to update it! – Casper Zandbergen Mar 07 '19 at 08:24
  • @CasperZandbergen Your answer is accurate. API unchanged from what I'm finding. `Bose Connect` app gets current song playing from bluetooth headset via A2DP or another bluetooth profile. – Ryderpro Mar 10 '19 at 00:18