I have audio player inside my app that allows users listen to the music. My app crashes when I'm trying to get time of audio file from URL.
Error is - Double value cannot be converted to Int because it is either infinite or NaN
I'm confused, as before it worked fine, but then I changed link to file and this crash appeared.
I tried changing the way I'm converting time but got same error. Also googling didn't help me much.
Here's full code of how I configure player:
func setupPlayer() {
let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
let duration: CMTime = playerItem.asset.duration
let seconds: Float64 = CMTimeGetSeconds(duration)
let mySecs = Int(seconds) % 60
let myMins = Int(seconds / 60)
let myTimes = String(format: "%02i", myMins) + ":" + String(format: "%02i", mySecs);
timeLabel.text = myTimes
timeSlider.setThumbImage(UIImage(), for: .normal)
timeSlider!.maximumValue = Float(seconds)
timeSlider!.isContinuous = false
timeSlider!.addTarget(self, action: #selector(AudioPlayer.playbackSliderValueChanged(_:)), for: .valueChanged)
player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
if self.player!.currentItem?.status == .readyToPlay {
let time : Float64 = CMTimeGetSeconds(self.player!.currentTime())
let mySecs2 = Int(time) % 60
let myMins2 = Int(time / 60)
let myTimes2 = String(format: "%02i", myMins2) + ":" + String(format: "%02i", mySecs2)
self.timeLabel.text = myTimes2 + " / " + myTimes
self.timeSlider!.value = Float ( time )
}
}
}
Most confusing thing for me, is that it gets current time without any issues, but full time of track - crashes.
Would be grateful if someone could help me fix this issue. Thanks in advance.