0

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

Image With Error

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.

Taras Tomchuk
  • 331
  • 7
  • 19
  • Besides the information in the duplicate, also consider using the properties of `CMTime` to check if the value is valid and to get the seconds instead of using `CMTimeGetSeconds`. – rmaddy Oct 13 '18 at 18:12
  • @rmaddy Hi, I have seen that question, but it doesn't solve the issue, it just explains how to show if value is legal or not. In my case, I need some solution, how to overcome this bug... – Taras Tomchuk Oct 13 '18 at 18:17

0 Answers0