Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current song playing in Spotify on iPhone

Tags:

ios

swift

spotify

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)")
        }

    }
like image 941
GarySabo Avatar asked Mar 05 '17 19:03

GarySabo


People also ask

How do I show my current song on Spotify?

Control what plays with the Now Playing bar at the bottom of the app. Tip: On mobile, find Now Playing just above the menu bar. Tap it for a bigger view. This helps us get to know you better so your recommendations improve.

Why is Spotify not showing the song playing?

If you have an Android phone and are experiencing the problem yourself, the best thing you can do is make sure automatic app updates are enabled on the Play Store. Open the Play Store on your phone, tap your profile icon in the top right, tap 'Settings,' tap 'Network preferences,' and tap 'Auto-update apps.

How do you see what songs you listened to on Spotify on iPhone?

2Access Spotify Listening History on iPhone App First, open the Spotify app and tap on the "Recently played" tab at the bottom of the screen. Next, tap on the history tab. Here you will see a list of all the songs you've listened to recently, as well as the date and time when you listened to them.


1 Answers

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.

like image 64
Casper Zandbergen Avatar answered Sep 22 '22 13:09

Casper Zandbergen