从Spotify iOS应用程序获取当前播放的歌曲

bud*_*ddy 30 spotify mpnowplayinginfocenter

我正在尝试获取有关Spotify iOS应用中当前播放歌曲的信息.方案如下:打开Spotify iOS应用程序,开始播放歌曲.将应用程序置于后台并打开我的iOS应用程序.有什么方法可以让我在iOS应用程序中的Spotify上播放这首歌吗?

我已尝试使用nowPlayingItem属性,如本文所述,但它不起作用:在iPhone上:找出当前正在播放的歌曲?(在iPod音乐播放器中)

我已尝试使用[MPNowPlayingInfoCenter defaultCenter] .nowPlayingInfo,如本文所述,但它不起作用:当iPhone连接到附件时,有没有办法访问当前播放的曲目?

我在苹果开发人员论坛上的帖子中看到了同样的问题:https://devforums.apple.com/message/903640#903640.

有谁遇到过这个问题?你找到了可行的解决方案吗?

谢谢,

Dev*_*n B 6

这是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)")
}
Run Code Online (Sandbox Code Playgroud)

请注意,从iOS 10开始,您必须NSAppleMusicUsageDescription在Info.plist中设置描述字符串,以便访问用户的音乐.

mediaItem将是一个MPMediaItem如果有什么玩的实例.您可以使用该value(forProperty:)方法访问媒体项的属性.此方法具有属性.这些属性在"通用媒体项属性键"标题下的"MPMediaItem"文档中定义.

  • 这仅适用于iOS播放器.使用像Spotify这样的第三方应用程序,`mediaItem`是'nil`. (2认同)

小智 0

如果我没记错的话,Apple 不允许程序共享数据(出于安全原因)。所以也许这就是问题所在。