MPNowPlayingInfoCenter has stopped updating the progress bar

I am creating an application that has a music player. I did not work on this after a while, but the last time I worked on it, nowPlayingInfo worked. Now that I upgraded it to Swift 3, everything works, except for the progress bar.

I saw several similar questions here that spoke about the state of the race, and that it was redefined by something, but I can’t understand that. I am using AVQueuePlayer if this helps.

Scrolling and previous buttons work fine, album art, title and artist, but the elapsed time is simply not updated. When I debug, MPNowPlayingInfoPropertyElapsedPlaybackTime from MPNowPlayingInfoCenter.default().nowPlayingInfo shows the correct numbers, but the actual screen in the control center is not. Usually progress is stuck at 0:01 and just does not move. However, if I try to use the slider in my application and return to the control center, it shows the time I was looking for, but I stay on that.

Here is my setNowPlaying code:

 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 } 

I used MPNowPlayingInfoPropertyElapsedPlaybackTime for the MPNowPlayingInfoPropertyElapsedPlaybackTime variable, which is passed to the method, but since it did not work, I tried player.currentTime() as recommended by other questions, and this is probably a better measure than what I used anyways.

Here is the search code if it helps:

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

For some reason, this is the only thing that will update the information of the control center.

+5
source share
1 answer

It worked for me, maybe it will help you.

  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)" } 
0
source

Source: https://habr.com/ru/post/1270686/


All Articles