我正在制作一款内置音乐播放器的应用。我有一段时间没有参与其中,但最后一次工作nowPlayingInfo
工作。现在,当我将其更新为Swift 3时,除进度条外,一切正常。
我在这里看到了一些类似的问题,谈到了竞争条件,它被某些东西所覆盖,但我似乎无法弄清楚是什么。如果有帮助,我正在使用AVQueuePlayer
。
跳过和上一个按钮工作,专辑封面,标题和艺术家完美更新,但经过的时间不更新。当我调试时,来自MPNowPlayingInfoPropertyElapsedPlaybackTime
的{{1}}显示正确的数字,但控制中心的实际屏幕却没有。通常,进度停留在0:01并且不会移动。但是,如果我寻求在我的应用程序中使用滑块并返回控制中心,它会显示我所寻找的时间,但仍保持此状态。
这是我的MPNowPlayingInfoCenter.default().nowPlayingInfo
代码:
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
}
设置为传递给方法的MPNowPlayingInfoPropertyElapsedPlaybackTime
变量,但由于它不起作用,我按照其他问题的推荐尝试了timePlayed
,它是可能比我正在使用的更好的措施。
以下是寻求帮助的代码:
player.currentTime()
出于某种原因,这是唯一可以更新控制中心信息的内容。
答案 0 :(得分: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)"
}
答案 1 :(得分:0)
func setNowPlaying(_ dura: Float, timePlayed: Float)
应该由Timer
调用。
func setNowPlaying(_ dura: Float, timePlayed: Float)
只是一种简单的方法,如果不调用它就不会更新。
您应该使用Timer
定期调用它,以更新当前播放时间。
通常,玩家走了,进度条跟随着。您可以使用相同的计时器方法调用func setNowPlaying(_ dura: Float, timePlayed: Float)