gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:31:14 25 4
gpt4 key购买 nike

我正在尝试获取有关 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 .

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

谢谢

最佳答案

这是 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”文档的“常规媒体项属性键”标题下定义。

关于spotify - 从 Spotify iOS 应用程序获取当前播放的歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com