音乐信息显示在“命令中心"中,但不显示在“锁定屏幕"中 [英] Music info showing in Command Center but not Lock Screen

查看:162
本文介绍了音乐信息显示在“命令中心"中,但不显示在“锁定屏幕"中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Swift 4+, iOS 11+, Xcode 10+

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

I've built a music player using MPMediaPlayer and I can interact with it from the Command Center, however I would like to be able to also see it on the Lock Screen.

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

To be honest, I'm a bit confused as to why it's showing/working in the Command Center as I have not written any code to do this. Nevertheless, I would also like it to show in the Lock Screen.

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

This is what I have done so far:

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

1) I'm using the applicationMusicPlayer and made certain something is playing during my tests:

MPMusicPlayerController.applicationMusicPlayer

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

2) Set the BackgroundModes to include Audio, Fetch, and Remote Notifications

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

3) Added AVAudioSession code (which doesn't seem to do anything as I have tried it and tried commenting it out and seen no difference):

  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)使用此基本代码查看我是否可以仅使用一些基本的硬编码内容将其显示在锁定屏幕上:

4) Used this basic code to see if I can get it to show on the lock screen with just some basic hard-coded content:

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

5) I know I have not implemented anything to actively update the info or respond to any commands as I'm just trying to get something to show on the lock screen at this point.

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

Why is the now playing info showing in the Command Center if I have done nothing to put it there?

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

What do I need to do to get the info to show on the Lock Screen like it does in the Command Center?

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

Link to simple project that has same issue on GitLab:https://gitlab.com/whoit/lockscreentest

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

I have submitted this as a bug to Apple, however they have yet to confirm or resolve this.

推荐答案

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

Using .systemMusicPlayer instead of .applicationMusicPlayer will solve your problem.

作为Apple文档:

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

The music player does not affect the Music app’s state. When your app moves to the background, the music player stops playing the current media.

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

And I think it's related to not showing in locked screen.

以及systemMusicPlayer处理所有歌曲信息以自动显示.

and also systemMusicPlayer handles all song informations to show automatically.

这篇关于音乐信息显示在“命令中心"中,但不显示在“锁定屏幕"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆