【发布时间】:2017-05-31 20:19:27
【问题描述】:
我正在使用 Swift 3 制作一个 iOS 应用程序,其中在锁定屏幕和控制中心上显示有关当前播放项目的信息会很好。
目前,我正在使用以下代码尝试将此信息插入到nowPlayingInfo 字典中。我还包含了对videoBeganPlaying(_:) 中使用的VideoInfo 类的引用。
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])
}
该函数被调用,并执行打印语句,输出Optional("title")。但是,控制中心和锁屏不会更新它们的信息。暂停/播放,并且向前跳按钮起作用,因为我使用 MPRemoteCommandCenter 在viewDidLoad() 中设置它们。
怎么了?
编辑:
正如 matt 指出的那样,AVPlayerViewController 让 MPNowPlayingInfoCenter 变得时髦。这是我的问题。我应该指定这是我正在使用的类,而不仅仅是AVPlayer。
【问题讨论】:
标签: ios swift swift3 mpnowplayinginfocenter