set*_*twi 6 ios mpnowplayinginfocenter swift swift3
我正在使用Swift 3创建一个iOS应用程序,其中显示有关锁定屏幕和控制中心当前正在播放的项目的信息将是不错的.
目前,我正在使用以下代码尝试将此信息插入到nowPlayingInfo字典中.我还包括对使用的VideoInfo类的引用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])
}
Run Code Online (Sandbox Code Playgroud)
该函数应该被调用,并且执行print语句,输出Optional("title").但是,控制中心和锁定屏幕不会更新其信息.暂停/播放,以及跳过前进按钮的工作,正如我在viewDidLoad()使用中设置它们MPRemoteCommandCenter.
出了什么问题?
编辑:
正如马特指出的那样,AVPlayerViewController使得MPNowPlayingInfoCenter时髦.这是我的问题.我应该指定这是我正在使用的类,而不仅仅是AVPlayer.
请参阅:https: //developer.apple.com/reference/avkit/avplayerviewcontroller/1845194-updatesnowplayinginfocenter
它确实有效,并且你不需要关于第一响应者的所有那些,等等,因为你可以通过继续设置现在播放的信息而不是其他任何事情来向自己证明:
那么为什么它不适合你呢?可能是因为您正在使用某种类型的播放器(例如AVPlayerViewController)以某种方式设置正在播放的信息本身,从而覆盖您的设置.
如果您正在使用AVPlayerViewController,您可以指定AVPlayerViewController实例不更新NowPlayingInfoCenter:
playerViewController.updatesNowPlayingInfoCenter = false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4911 次 |
| 最近记录: |