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.
有谁遇到过这个问题?你找到了可行的解决方案吗?
谢谢,
这是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"文档中定义.
归档时间: |
|
查看次数: |
4417 次 |
最近记录: |