32

I'm trying to get info about the current playing song in Spotify iOS app. The scenario is as follows: Open Spotify iOS app, start playing a song. Put the app in background and open my iOS app. Is there any way I could get the song playing on Spotify, in my iOS app?

I have tried using the nowPlayingItem property as described in this post, but it didn't work: On iPhone: Find out what song is currently playing? (in the iPod music player)

I have tried using the [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo as described in this post, but it didn't work: Is there a way to access the currently played track while the iPhone is connected to an accessory?

I have seen the same question asked in this post on apple developer forums: https://devforums.apple.com/message/903640#903640.

Has anyone encountered this problem? Have you found a viable solution?

Thanks,

Community
  • 1
  • 1
buddy
  • 329
  • 3
  • 5
  • possible duplicate of [Determine song being played on iPhone by third-party app](http://stackoverflow.com/questions/8542854/determine-song-being-played-on-iphone-by-third-party-app) – feco Jun 24 '15 at 07:53
  • I don't believe this is a duplicate because that question is asking for a generic solution, so any answers to that question can not take into account any possibly Spotify-specific APIs. – Emil Vikström Sep 07 '15 at 09:47
  • This may help you: [Get meta data displayed in MPNowPlayingInfoCenter's nowPlayingInfo](http://stackoverflow.com/questions/20534939/get-meta-data-displayed-in-mpnowplayinginfocenters-nowplayinginfolock-screen-a) – Devin Brown Nov 17 '15 at 21:12

4 Answers4

7

Here is a solution in Swift 3:

let player = MPMusicPlayerController.systemMusicPlayer()
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)")
}

Note that starting from iOS 10, you must set a description string for NSAppleMusicUsageDescription in the Info.plist in order to get access to the user's music.

mediaItem will be an instance of MPMediaItem if there is something playing. You can access the media item's attributes using the value(forProperty:) method. This methods takes a property. The properties are defined in the documentation for `MPMediaItem under the heading "General Media Item Property Keys".

fabian789
  • 7,991
  • 4
  • 39
  • 88
Devin Brown
  • 1,006
  • 10
  • 15
  • 2
    This only works with the iOS player. With third-party apps like Spotify, `mediaItem` is `nil`. – Rob Jan 29 '17 at 16:21
  • I'm looking for this too, is there anyway to access 3rd party apps' now playing info? – GarySabo Mar 06 '17 at 16:11
1

With the latest version of the Spotify iOS SDK (introduced around Sep 2018) you can now subscribe for player events and detect when a new track starts playing.

For more info check their the quick-start tutorial.

Pedro
  • 2,653
  • 2
  • 19
  • 16
0

If I'm not wrong, Apple doesn't allow program's to share data (for security reasons). So maybe that's the problem.

Slimby
  • 81
  • 8
-1

First you need to connect spotify app to your app then you can get from SPTAppRemotePlayerState?.track.uri from this you get uri