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