使用MPRemoteCommandCenter为AVPlayer和AVAudioPlayer添加当前时间和持续时间的位置 [英] Where to Add Current Time and Duration Time For AVPlayer and AVAudioPlayer using MPRemoteCommandCenter

查看:42
本文介绍了使用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添加当前时间和持续时间的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆