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.