我正在开发像 Netflix 这样的应用程序,当用户观看视频时,他可以锁定屏幕并查看带有视频内容的锁定屏幕控制器。
问题 1
当应用程序回到前台时,用户退出播放器,再次锁定屏幕,但播放器控制器仍处于锁定屏幕中。
问题 2
用户观看视频后,退出播放器并进入另一个视频,当他锁定屏幕时,播放器锁屏控制器播放 2 个视频,当前一个和之前的一个。
据我了解,由于 MPRemoteCommandCenter 完成方法(playCommand、pauseCommand 等),播放器永远不会从内存中释放。
我的自定义播放器类设置锁屏控制器:
private var nowPlayingInfo : [String:Any] = [:]
//MARK: UPDATING LOCKSCREEN STATUS
func updateLockscreenInfo(playPosition: Double? = nil) {
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = currentItem?.duration.seconds ?? 0
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playPosition ?? currentTime().seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
//MARK: LOCKSCREEN PLAYER DELEGATES
private func setupLockScreen() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { [weak self] event -> MPRemoteCommandHandlerStatus in
print(self)
//after playing 2 videos, prints twixe 2 different instances
self?.play()
self?.updateLockscreenInfo()
return .success
}
}
//MARK: LOCK SCREEN INITIAL SETTER ( IMAGE & TITLE )
private func setControllerLockScreen(imageURL: String?, title: String?) {
nowPlayingInfo[MPMediaItemPropertyTitle] = title
if let imageURL = imageURL, let url = URL(string: imageURL) {
var data : Data?
do {
data = try Data(contentsOf: url)
if let data = data, let image = UIImage(data: data) {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size, requestHandler: { (size) -> UIImage in
return image
})
}
}
catch {
po(name: "error downloading image: ", data: error.localizedDescription)
}
}
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
当我删除“commandCenter.playCommand.addTarget”时,锁定屏幕甚至不显示。