MPNowPlayingInfoCenter 在分配给 nowPlayingInfo 后不更新任何信息 [英] MPNowPlayingInfoCenter doesn't update any information when after assigning to nowPlayingInfo

查看:49
本文介绍了MPNowPlayingInfoCenter 在分配给 nowPlayingInfo 后不更新任何信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swift 3 制作一个 iOS 应用,其中在锁定屏幕和控制中心上显示有关当前播放项目的信息会很好.

目前,我正在使用以下代码尝试将此信息插入到 nowPlayingInfo 字典中.我还引用了 videoBeganPlaying(_:) 中使用的 VideoInfo 类.

class VideoInfo {var 频道名称:字符串变量名称:字符串}//...var videoInfoNowPlaying: VideoInfo?//...@objc private func videoBeganPlaying(_ 通知:NSNotification?) {//显然这些必须在这里才能工作......但它没有UIApplication.shared.beginReceivingRemoteControlEvents()self.becomeFirstResponder()守卫让信息 = self.videoInfoNowPlaying else { return }让艺术品 = MPMediaItemArtwork(boundsSize: .zero, requestHandler:{ (_) ->UIImage in #imageLiteral(resourceName: "DefaultThumbnail") })//这是填充物MPNowPlayingInfoCenter.default().nowPlayingInfo = [MPMediaItemPropertyTitle: info.title,MPMediaItemPropertyArtist: info.channelName,MPMediaItemPropertyArtwork:艺术品]print("当前标题:", MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle])}

函数在它应该调用的时候被调用,打印语句被执行,输出Optional("title").但是,控制中心和锁屏不会更新它们的信息.暂停/播放和向前跳过按钮工作,因为我在 viewDidLoad() 中使用 MPRemoteCommandCenter 设置它们.

怎么了?

正如马特指出的那样,AVPlayerViewController 使 MPNowPlayingInfoCenter 变得时髦.这是我的问题.我应该指定这是我正在使用的类,而不仅仅是 AVPlayer.

见:

那为什么它不适合你呢?可能是因为您正在使用某种播放器(例如 AVPlayerViewController)以某种方式设置正在播放的信息本身,从而覆盖您的设置.

I'm making an iOS app using Swift 3 in which displaying information about the currently playing item on the lock screen and control center would be nice.

Currently, I'm using the following code to attempt to insert this information into the nowPlayingInfo dictionary. I've also included reference to the VideoInfo class that is used in videoBeganPlaying(_:).

class VideoInfo {
    var channelName: String
    var title: String
}

// ...

var videoInfoNowPlaying: VideoInfo?

// ...

@objc private func videoBeganPlaying(_ notification: NSNotification?) {
    // apparently these have to be here in order for this to work... but it doesn't
    UIApplication.shared.beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()
    guard let info = self.videoInfoNowPlaying else { return }
    let artwork = MPMediaItemArtwork(boundsSize: .zero, requestHandler:
        { (_) -> UIImage in #imageLiteral(resourceName: "DefaultThumbnail") }) // this is filler
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [
            MPMediaItemPropertyTitle: info.title,
            MPMediaItemPropertyArtist: info.channelName,
            MPMediaItemPropertyArtwork: artwork
        ]
    print("Current title: ", MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle])
}

The function is called when it should be, and the print statement executed, outputting Optional("title"). However, the control center and lockscreen do not update their information. Pause/play, and the skip forward button work, as I set them in viewDidLoad() using MPRemoteCommandCenter.

What's going wrong?

EDIT:

As matt pointed out, AVPlayerViewController makes the MPNowPlayingInfoCenter funky. This was my issue. I should have specified that this is the class I am using, not just AVPlayer.

See: https://developer.apple.com/reference/avkit/avplayerviewcontroller/1845194-updatesnowplayinginfocenter

解决方案

It does work, and you don't need all that glop about the first responder and so on, as you can readily prove to yourself by just going ahead and setting the now playing info and nothing else:

So why isn't it working for you? Probably because you are using some sort of player (such as AVPlayerViewController) that sets the now playing info itself in some way, and thus overrides your settings.

这篇关于MPNowPlayingInfoCenter 在分配给 nowPlayingInfo 后不更新任何信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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