在 iPhone 上获取当前在 Spotify 中播放的歌曲
Get current song playing in Spotify on iPhone
访问 MPMusicPlayerController.systemMusicPlayer()
(下面的代码)可以获取 Apple Music 应用程序中正在播放的歌曲的曲目信息,但是我们有没有办法可以访问 Spotify 应用程序中正在播放的歌曲的信息?
此答案中发布的代码 I need to know how to get information about which player is currently streaming (player, spotify, napster...) 使用 MPNowPlayingInfoCenter
无论是使用 Apple Music 还是 Spotify 等,它都是 nil
let player = MPMusicPlayerController.systemMusicPlayer()
@IBAction func getMusicButton(_ sender: UIButton) {
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)")
}
}
Apple 文档指出有两种类型的音乐播放器:
- An application music player plays music locally within your app. It is not aware of the Music app’s now-playing item, nor does it
affect the Music app’s state. There are two application music players:
applicationMusicPlayer
and applicationQueuePlayer
. The application
queue player provides greater control over the contents of the queue
and is the preferred player.
- The system music player employs the built-in Music app on your behalf. On instantiation, it takes on the current Music app state,
such as the identification of the now-playing item. If a user switches
away from your app while music is playing, that music continues to
play. The Music app then has your music player’s most recently-set
repeat mode, shuffle mode, playback state, and now-playing item.
第一个仅在您的应用程序中,第二个是 Apple Music 应用程序,因为您无法在普通应用程序中超出应用程序的沙箱,这在 App Store 应用程序中是不可能的。
如果您仍然想创建您的想法,您可以在越狱设备上开发它。 This github project has an easy example of how to get the current playing song in a jailbreak tweak. If you want to try it, this 是关于如何开始开发调整的优秀教程,虽然有点旧,但仍然很准确。
更新
我只是 运行 进入 this Spotify SDK,这将让用户登录并播放用户的 Spotify 音乐。这样音乐流将在您的应用程序中,您可以访问 applicationMusicPlayer 中的所有信息。不利之处在于,与仅获取有关 Spotify 播放曲目的信息相比,这将需要更多的设置工作,并且用户将不得不从您的应用程序而不是 Spotify 应用程序播放他的音乐。
访问 MPMusicPlayerController.systemMusicPlayer()
(下面的代码)可以获取 Apple Music 应用程序中正在播放的歌曲的曲目信息,但是我们有没有办法可以访问 Spotify 应用程序中正在播放的歌曲的信息?
此答案中发布的代码 I need to know how to get information about which player is currently streaming (player, spotify, napster...) 使用 MPNowPlayingInfoCenter
无论是使用 Apple Music 还是 Spotify 等,它都是 nil
let player = MPMusicPlayerController.systemMusicPlayer()
@IBAction func getMusicButton(_ sender: UIButton) {
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)")
}
}
Apple 文档指出有两种类型的音乐播放器:
- An application music player plays music locally within your app. It is not aware of the Music app’s now-playing item, nor does it affect the Music app’s state. There are two application music players:
applicationMusicPlayer
andapplicationQueuePlayer
. The application queue player provides greater control over the contents of the queue and is the preferred player.- The system music player employs the built-in Music app on your behalf. On instantiation, it takes on the current Music app state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The Music app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item.
第一个仅在您的应用程序中,第二个是 Apple Music 应用程序,因为您无法在普通应用程序中超出应用程序的沙箱,这在 App Store 应用程序中是不可能的。
如果您仍然想创建您的想法,您可以在越狱设备上开发它。 This github project has an easy example of how to get the current playing song in a jailbreak tweak. If you want to try it, this 是关于如何开始开发调整的优秀教程,虽然有点旧,但仍然很准确。
更新
我只是 运行 进入 this Spotify SDK,这将让用户登录并播放用户的 Spotify 音乐。这样音乐流将在您的应用程序中,您可以访问 applicationMusicPlayer 中的所有信息。不利之处在于,与仅获取有关 Spotify 播放曲目的信息相比,这将需要更多的设置工作,并且用户将不得不从您的应用程序而不是 Spotify 应用程序播放他的音乐。