MPNowPlayingInfoCenter停止更新进度条

lag*_*oon 6 avqueueplayer mpnowplayinginfocenter swift swift3

我正在制作一个有音乐播放器的应用程序.我有一段时间没有参与其中,但是我最后一次工作时它nowPlayingInfo 正在工作.现在,当我将其更新为Swift 3时,除进度条外,一切正常.

我在这里看到了几个类似的问题,谈到了竞争条件,它被某些东西所覆盖,但我似乎无法弄清楚是什么.我正在使用AVQueuePlayerif如果有帮助.

跳过和上一个按钮工作,专辑封面,标题和艺术家完美更新,但经过的时间只是没有更新.当我调试时,MPNowPlayingInfoPropertyElapsedPlaybackTimefrom MPNowPlayingInfoCenter.default().nowPlayingInfo显示正确的数字,但控制中心的实际屏幕不是.通常,进度停留在0:01并且不会移动.但是,如果我在我的应用程序中寻求使用滑块并返回控制中心,它会显示我寻找的时间但仍然保持不变.

这是我的setNowPlaying代码:

func setNowPlaying(_ dura: Float, timePlayed: Float) {
    let s = songs[playbackInstance.currentSongIndex]
    let albumArt = MPMediaItemArtwork.init(boundsSize: CGSize(width: 480, height: 360), requestHandler: { (size) -> UIImage in
        return self.songImageView.image ?? #imageLiteral(resourceName: "WhiteMusic")
    })
    let songInfo: [String: Any]? = [
        MPMediaItemPropertyTitle: s.name,
        MPMediaItemPropertyArtist: s.artist,
        MPMediaItemPropertyArtwork: albumArt,
        MPMediaItemPropertyPlaybackDuration: dura,
        MPNowPlayingInfoPropertyElapsedPlaybackTime: CMTimeGetSeconds(player.currentTime())
    ]

    MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo
}
Run Code Online (Sandbox Code Playgroud)

我曾经设置过传递给方法MPNowPlayingInfoPropertyElapsedPlaybackTimetimePlayed变量,但由于它不起作用我player.currentTime()按照其他问题的建议尝试,这可能是比我正在使用的更好的衡量标准.

以下是寻求帮助的代码:

@IBAction func sliderChanged(_ sender: UISlider) {
    if timer.isValid {
        currentTimeLabel.text = secondsToText(sender.value)
        player.seek(to: CMTimeMakeWithSeconds(Float64(sender.value), player.currentItem!.currentTime().timescale))
    }
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,这是唯一可以更新控制中心信息的东西.

cwi*_*msz 0

这对我有用,也许会对你有帮助。

         func setNowPlaying(_ dura: Float, timePlayed: Float) {
            let s = songs[playbackInstance.currentSongIndex]
            let albumArt = MPMediaItemArtwork.init(boundsSize: CGSize(width: 480, height: 360), requestHandler: { (size) -> UIImage in
                return self.songImageView.image ?? #imageLiteral(resourceName: "WhiteMusic")
            })

            MPNowPlayingInfoCenter.default().nowPlayingInfo = [
                MPMediaItemPropertyTitle: s.name,
                MPMediaItemPropertyArtist: s.artist,
                MPMediaItemPropertyArtwork: albumArt as Any,
                MPMediaItemPropertyPlaybackDuration: dura,
                MPNowPlayingInfoPropertyElapsedPlaybackTime : player.currentTime()
            ]
            UIApplication.shared.beginReceivingRemoteControlEvents()
            becomeFirstResponder()

        }


        @IBAction func sliderAction(_ sender: Any) {
                if player != nil {
                    Thread.cancelPreviousPerformRequests(withTarget: self)
                    self.perform(#selector(playAtSelectedTime) , with: nil, afterDelay: 0.2)
                }
            }

        @objc func playAtSelectedTime(){
                let selectedTime = Double(progressSlider.value)
                player.currentTime = selectedTime
                convertTimingToTextLabel(selectedTime, label: self.runningLabel)
                convertTimingToTextLabel(Double(player.duration-player.currentTime), label: self.durationLabel)
                durationLabel.text = " -"+durationLabel.text!
                updateSlider()
            }   


        func updateSlider(){
                if timer != nil{
                    timer.invalidate()
                }

                timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(playingSong) , userInfo: nil, repeats: true)
            }


        @objc func playingSong(){
                progressSlider.minimumValue = 0
                progressSlider.maximumValue = Float(player.duration)
                progressSlider.value = Float(player.currentTime)

                convertTimingToTextLabel(Double(player.currentTime), label: self.runningLabel)
                convertTimingToTextLabel(Double(player.duration-player.currentTime), label: self.durationLabel)
                durationLabel.text = durationLabel.text!
            }



            func convertTimingToTextLabel(_ time: Double, label: UILabel) {
                    let minute = Int(time / 60)
                    let seconds = Int(time - Double(minute * 60))
                    setTimingSongForLabel(minute, seconds: seconds, label: label)
                }

            func setTimingSongForLabel(_ minute: Int, seconds: Int, label: UILabel) {
                    let mStr = minute > 9 ? "\(minute)":"0\(minute)"
                    let sStr = seconds > 9 ? "\(seconds)":"0\(seconds)"
                    label.text = "\(mStr):\(sStr)"
                }
Run Code Online (Sandbox Code Playgroud)