Where to Add Current Time and Duration Time For AVPlayer and AVAudioPlayer using MPRemoteCommandCenter(使用MPRemoteCommandCenter为AVPlayer和AVAudioPlayer添加当前时间和持续时间的位置)
问题描述
在我的应用程序内的一个屏幕上,我既有一个用于音乐的AVAudioPlayer,也有一个用于视频的AVPlayer。用户可以交换不同的歌曲和不同的视频,但一次只能播放一个。他们既可以播放音频播放器,也可以在avPlayer上观看视频。
我有一个MPRemoteCommandCenter,在使用暂停/播放/ff/倒带时,它对两者都很好地工作。问题是我不能在锁定屏幕上显示任何一个的CurrentTime或持续时间。我尝试了this,但它没有说明将代码放在哪里。
这就是我尝试的方法,以便每次用户切换歌曲或视频时,我都拥有新项目的所有可用数据:
音频-
do {
audioPlayer = try AVAudioPlayer(contentsOf: audioTrack)
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
audioPlayer?.play()
setupNowPlayingForAudio()
} catch {
}
func setupNowPlayingForAudio() {
guard let audioPlayer = audioplayer else { return }
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "My App Name"
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Float(audioPlayer.currentTime)
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = Float(audioPlayer.duration)
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = audioPlayer.rate
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
视频-
playerStatusObserver = player?.observe(.currentItem?.status, options: [.new, .old]) {
switch (player.status) {
case .readyToPlay:
player?.play()
setupNowPlayingForVideo()
}
}
func setupNowPlayingForVideo() {
guard let player = player, let playerItem = player.currentItem else { return }
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "My App Name"
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem.currentTime().seconds
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
MPRemoteCommandCenter与AVAudioSession一起在viewDidLoad中设置
推荐答案
我遵循了answer,这说明您必须将其添加到任何暂停/播放/ff/倒带按钮、滑块和任何收听玩家播放/停止的观察器中。以下是我做这件事的方式。这对我来说在锁屏上很好用。
这里是我对AudioPlayer
-
do {
audioPlayer = try AVAudioPlayer(contentsOf: audioTrack)
// ...
audioPlayer?.play()
setupNowPlaying(musicPlayer: audioPlayer)
} catch { }
func audioPlayerPausePlayButton() {
// ...
setupNowPlaying(musicPlayer: audioPlayer)
}
func audioPlayerFastFowardAndRewindButton() {
// ... ff or rewind functions
setupNowPlaying(musicPlayer: audioPlayer)
}
这里是我对AVPlayer
-
playerStatusObserver = player?.observe(.currentItem?.status, options: [.new, .old]) { // ...
switch (player.status) {
case .readyToPlay:
// ... this should occur on the MainQueue
self?.player?.play()
self?.setupNowPlaying(videoPlayer: self?.player)
}
}
// .. I also added it to any other observers that listen to the player stopping
func videoPlayerPausePlayButton() {
// ...
setupNowPlaying(videoPlayer: player)
}
func videoPlayerFastFowardAndRewindButton() {
// ...
player?.seek(to: whateverSeekTime) { [weak self](_) in
self?.setupNowPlaying(videoPlayer: self?.player)
}
}
要在锁定屏幕上显示的CommandCenter的字典值
func setupNowPlaying(musicPlayer: AVAudioPlayer? = nil, videoPlayer: AVPlayer? = nil) {
var nowPlayingInfo = [String : Any]()
// audio
if let musicPlayer = musicPlayer, let musicUrl = musicPlayer.url {
nowPlayingInfo[MPNowPlayingInfoPropertyAssetURL] = musicUrl
nowPlayingInfo[MPMediaItemPropertyTitle] = musicUrl.lastPathComponent
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType.audio.rawValue
let currentTime: TimeInterval = musicPlayer.currentTime
let musicCurrentTimeCMTime = CMTime(seconds: currentTime, preferredTimescale: 1000)
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(musicCurrentTimeCMTime)
let musicDuration: TimeInterval = musicPlayer.duration
let musicDurationCMTime = CMTime(seconds: musicDuration, preferredTimescale: 1000)
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = CMTimeGetSeconds(musicDurationCMTime)
if musicPlayer.isPlaying {
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1
} else {
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0
}
}
// video
if let videoPlayer = videoPlayer, let currentItem = videoPlayer.currentItem {
if let videoAsset = currentItem.asset as? AVURLAsset {
let videoUrl = videoAsset.url
nowPlayingInfo[MPNowPlayingInfoPropertyAssetURL] = videoUrl
nowPlayingInfo[MPMediaItemPropertyTitle] = videoUrl.lastPathComponent
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType.video.rawValue
}
if let videoDuration: CMTime = videoPlayer.currentItem?.duration {
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = CMTimeGetSeconds(videoDuration)
}
let videoCurrentTime: CMTime = videoPlayer.currentTime()
let videoCurrentTimeAsSecs = CMTimeGetSeconds(videoCurrentTime)
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = videoCurrentTimeAsSecs
print("videoCurrentTimeAsSecs: ", videoCurrentTimeAsSecs)
if videoPlayer.isPlaying {
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1
} else {
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0
}
}
nowPlayingInfo[MPMediaItemPropertyTitle] = "Your App Name"
nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = false
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
这篇关于使用MPRemoteCommandCenter为AVPlayer和AVAudioPlayer添加当前时间和持续时间的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用MPRemoteCommandCenter为AVPlayer和AVAudioPlayer添加当前时间和持续时间的位置
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01