【问题标题】:Get current playing song from Spotify iOS app从 Spotify iOS 应用程序获取当前正在播放的歌曲
【发布时间】:2013-12-29 00:22:16
【问题描述】:

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

我已尝试使用本文所述的 nowPlayingItem 属性,但没有成功:On iPhone: Find out what song is currently playing? (in the iPod music player)

我已尝试使用本文所述的 [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo,但它不起作用:Is there a way to access the currently played track while the iPhone is connected to an accessory?

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

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

谢谢,

【问题讨论】:

标签: spotify mpnowplayinginfocenter


【解决方案1】:

这是 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 开始,您必须在 Info.plist 中为 NSAppleMusicUsageDescription 设置描述字符串才能访问用户的音乐。

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

【讨论】:

  • 这仅适用于 iOS 播放器。对于 Spotify 等第三方应用,mediaItemnil
  • 我也在找这个,有什么办法可以访问第三方应用的正在播放信息吗?
【解决方案2】:

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

欲了解更多信息,请查看他们的the quick-start tutorial

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 你应该提供你从哪里获取的信息。
    【解决方案4】:

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

    【讨论】:

    • 试着在你的答案中提供更多细节,也许是一个更好的例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2015-05-17
    • 2016-02-26
    • 2019-05-15
    • 2019-12-26
    相关资源
    最近更新 更多