Get current playing song from Spotify iOS app

末鹿安然 提交于 2019-11-30 02:53:40
Devin B

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".

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!