33

I'm trying to get info about the current playing song in Spotify iOS app. The scenario is as follows: Open Spotify iOS app, start playing a song. Put the app in background and open my iOS app. Is there any way I could get the song playing on Spotify, in my iOS app?

I have tried using the nowPlayingItem property as described in this post, but it didn't work: On iPhone: Find out what song is currently playing? (in the iPod music player)

I have tried using the [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo as described in this post, but it didn't work: Is there a way to access the currently played track while the iPhone is connected to an accessory?

I have seen the same question asked in this post on apple developer forums: https://devforums.apple.com/message/903640#903640.

Has anyone encountered this problem? Have you found a viable solution?

Thanks,

4

4 回答 4

8

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

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

mediaItem将是MPMediaItem如果有东西在播放的一个实例。您可以使用该value(forProperty:)方法访问媒体项目的属性。此方法需要一个属性。这些属性在“通用媒体项属性键”标题下的“MPMediaItem”文档中定义。

于 2015-11-18T17:34:08.550 回答
1

借助最新版本的 Spotify iOS SDK(2018 年 9 月左右推出),您现在可以订阅播放器事件并检测新曲目何时开始播放。

有关更多信息,请查看他们的快速入门教程

于 2019-09-27T17:51:01.583 回答
0

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

于 2015-09-19T13:07:05.687 回答
-1

首先,您需要将 Spotify 应用程序连接到您的应用程序,然后您可以从中 SPTAppRemotePlayerState?.track.uri获取 uri

于 2021-04-20T08:52:22.057 回答