8

我正在制作一个包含音乐播放器的应用程序。我有一段时间没有工作了,但我最后一次工作时它nowPlayingInfo 正在工作。现在,当我将它更新到 Swift 3 时,除了进度条之外,一切正常。

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

跳过和上一个按钮有效,专辑封面、标题和艺术家更新完美,但经过的时间没有更新。当我调试时,MPNowPlayingInfoPropertyElapsedPlaybackTimeMPNowPlayingInfoCenter.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
}

我曾经设置为传递给该方法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))
    }
}

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

4

2 回答 2

0

func setNowPlaying(_ dura: Float, timePlayed: Float)应该由 a 调用Timer

func setNowPlaying(_ dura: Float, timePlayed: Float)只是一个简单的方法,如果你不调用它就不会更新。

您应该使用 aTimer定期调用它,以更新当前播放时间。

很多时候,玩家走了,进度条随之而来。您可以调用 func setNowPlaying(_ dura: Float, timePlayed: Float)相同的计时器方法

于 2020-11-23T03:07:41.707 回答
-1

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

         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)"
                }
于 2018-02-10T05:51:39.717 回答