【发布时间】:2017-09-03 02:12:45
【问题描述】:
我有一个可以从 iOS 命令中心和锁定屏幕播放的视频播放器。当我在我的应用程序中切换播放/暂停按钮时,它应该通过更新nowPlayingInfo (MPNowPlayingInfoCenter) 来更新命令中心 (MPRemoteCommandCenter) 中的播放/暂停按钮。我不确定为什么它不更新。
例如,如果我在应用中使用自定义按钮暂停视频,命令中心仍会显示暂停按钮(表示视频仍在播放,这是错误的。)
这就是我更新nowPlayingInfo的方式:
func updateMPNowPlayingInforCenterMetadata() {
guard video != nil else {
nowPlayingInfoCenter.nowPlayingInfo = nil
return
}
var nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo ?? [String: Any]()
let image: UIImage
if let placeholderLocalURL = video.placeholderLocalURL, let placeholderImage = UIImage(contentsOfFile: placeholderLocalURL.path) {
image = placeholderImage
} else {
image = UIImage()
}
let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: { _ -> UIImage in
return image
})
nowPlayingInfo[MPMediaItemPropertyTitle] = video.title
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = video.creator?.name ?? " "
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = Float(video.duration)
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Float(currentTime) // CMTimeGetSeconds(player.currentItem!.currentTime())
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = player.rate
nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo
if player.rate == 0.0 {
state = .paused
} else {
state = .playing
}
}
使用KVO,当玩家的rate发生变化时,我调用这个函数:
// MARK: - Key-Value Observing Method
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &assetPlaybackManagerKVOContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
} else if keyPath == #keyPath(AVPlayer.rate) {
updateMPNowPlayingInforCenterMetadata()
}
}
有什么想法吗?
更新
我找到了一个解决方案,但在我的情况下并不完美。所以在我的应用程序中,我有 2 个视图控制器。我们称他们为FeedVC 和PlayerVC。所以FeedVC 有AVPlayer 总是在播放但被静音。如果单击其中一个,则会创建 PlayerVC 并播放完整视频。 如果我在转到PlayerVC 之前暂停FeedVC 中的AVPlayer,那么NowPlayingInfoCenter 中的“播放/暂停”按钮将完美运行!
有没有办法让这项工作无需暂停FeedVC 中的视频?
另一个问题是,如果我不暂停 FeedVC 中的玩家,elapsed time 会继续计数。如果有多个玩家在玩,播放/暂停按钮和经过的时间似乎不正确。
【问题讨论】:
-
我得到了你,伙计,给我时间回家,我会告诉你我的解决方案。
-
我也面临同样的问题..你找到解决方案了吗
标签: ios objective-c swift mpmediaitem mpnowplayinginfocenter