I am making an app that has a music player in it. I haven't worked on it in a while but the last time I worked on it the nowPlayingInfo
was working. Now, when I updated it to Swift 3, everything works except for the progress bar.
I have seen a few similar questions on here that talked about race conditions and it being overridden by something but I can't seem to figure out by what. I am using an AVQueuePlayer
if that helps.
The skip and previous buttons work, the album art, title, and artist update perfectly but the elapsed time just isn't updating. When I debug, the MPNowPlayingInfoPropertyElapsedPlaybackTime
from MPNowPlayingInfoCenter.default().nowPlayingInfo
is showing the correct numbers, but the actual screen in control center is not. Usually, the progress is stuck at 0:01 and just does not move. However, if I seek using the slider in my app and go back to control center, it shows the time I seeked to but stays at 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 to set MPNowPlayingInfoPropertyElapsedPlaybackTime
to the timePlayed
variable that is passed in to the method, but since it wasn't working I tried player.currentTime()
as recommended by other questions and it is probably a better measure than what I was using anyways.
Here is the code for seeking in case 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 that is the only thing that will update the control center info.