1

MPMusicPlayerController.systemMusicPlayer.nowPlayingItem只有在音乐应用程序被杀死并重新打开后才改变状态。具体来说,

  1. 打开音乐应用程序并播放音乐。
  2. 打开我创建的应用程序。它正在使用MPMusicPlayerController.systemMusicPlayer.nowPlayingItem. 确认nowPlayingItem数据正确(包括当前播放的音乐数据)。
  3. 切换到音乐应用程序并杀死。然后,重新打开音乐应用程序并更改音乐。
  4. 切换到我的应用程序并显示nowPlayingItem数据。我想要当前的音乐数据,但它是旧的音乐数据。
// Run SceneDelegate when foreground that my app.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
        player = MPMusicPlayerController.systemMusicPlayer
        print(player?.nowPlayingItem?.title)

这是在 iOS13.3 上运行的正确代码。更新到iOS13.4后,无法正常运行。

4

0 回答 0