音乐信息显示在命令中心,但不显示在锁定屏幕上

时间:2019-01-13 17:14:44

标签: ios swift mpnowplayinginfocenter mpmediaplayercontroller mpremotecommandcenter

使用Swift 4 +,iOS 11 +,Xcode 10 +

我已经使用MPMediaPlayer构建了一个音乐播放器,并且可以从命令中心与之进行交互,但是我也希望能够在锁定屏幕上看到它。

说实话,我对为什么它在Command Center中显示/工作有些困惑,因为我没有编写任何代码来做到这一点。 不过,我也希望它显示在锁定屏幕中。

这是我到目前为止所做的:

1)我正在使用applicationMusicPlayer并确定在测试期间正在播放某些东西:

MPMusicPlayerController.applicationMusicPlayer

2)将BackgroundModes设置为包括音频,提取和远程通知

enter image description here

3)添加了AVAudioSession代码(似乎没有做任何事情,因为我已经尝试过并尝试将其注释掉,并且没有区别):

  let session = AVAudioSession.sharedInstance()
    do {
        // Configure the audio session for playback
        try session.setCategory(AVAudioSessionCategoryPlayback,
                                mode: AVAudioSessionModeDefault,
                                options: [])
        try session.setActive(true)
    } catch let error as NSError {
        print("Failed to set the audio session category and mode: \(error.localizedDescription)")
    }

4)使用此基本代码查看我是否可以仅使用一些基本的硬编码内容将其显示在锁定屏幕上:

let nowPlayingInfo: [String: Any] = [
    MPMediaItemPropertyArtist: "Pink Floyd",
    MPMediaItemPropertyTitle: "Wish You Were Here",
    //MPMediaItemPropertyArtwork: mediaArtwork,
]
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()

5)我知道我还没有实施任何措施来主动更新信息或响应任何命令,因为我此时正试图在锁定屏幕上显示一些内容。

如果我什么都没做,为什么现在正在命令中心显示正在播放的信息?

我需要做些什么才能使信息像在“命令中心”中一样显示在锁定屏幕上?

编辑: 链接到在GitLab上存在相同问题的简单项目:https://gitlab.com/whoit/lockscreentest

编辑:我已将此错误提交给Apple,但他们尚未确认或解决。

2 个答案:

答案 0 :(得分:0)

我必须至少填充4个键(即使是空字符串),才能在锁定屏幕上看到正确的内容:

  • MPMediaItemPropertyTitle
  • MPMediaItemPropertyArtist
  • MPMediaItemPropertyAlbumTitle
  • MPNowPlayingInfoPropertyPlaybackRate

您可以检查此NowPlayingSource code source

答案 1 :(得分:0)

使用.systemMusicPlayer代替.applicationMusicPlayer将解决您的问题。

作为Apple文档:

  

音乐播放器不会影响“音乐”应用的状态。当您的应用移至后台时,音乐播放器将停止播放当前媒体。

我认为这与在锁定屏幕中不显示有关。

还有systemMusicPlayer处理所有歌曲信息以自动显示。

相关问题