danu
danu

Reputation: 1188

Double value cannot be converted to Int64 because it is either infinite or NaN

Im new to swift and I'm working on a project in swift 3.0 where I have a UISlider to drag. Once I drag it i get the following crash saying "Double value cannot be converted to Int64 because it is either infinite or NaN". My code as bellow. How would I overcome this bug ??. The line that getting crashed is "let seekTime = CMTime(value: Int64(val), timescale: 1)"...

@IBAction func sliderValueChanged(_ sender: UISlider) {

    if let duration = player?.currentItem?.duration {

        let totalsecs = CMTimeGetSeconds(duration)
        print("Durtion :",totalsecs)
        let val = Float64(sender.value) * totalsecs

        let totalduration = CMTimeGetSeconds(duration) as Float64

        if(val != totalduration){

            let seekTime = CMTime(value: Int64(val), timescale: 1)
            player?.seek(to: seekTime, completionHandler: { (completedSeek) in

            })
        }else{
            print("Slider dragged error")
        }
    }
}

Upvotes: 0

Views: 3012

Answers (1)

DonMag
DonMag

Reputation: 77638

Try to follow the error message...

"Double value cannot be converted to Int64 because it is either infinite or NaN" - and you say it's on the let seekTime = CMTime(value: Int64(val), timescale: 1) line...

So... since that line is using Int64(val) it's pretty clear that is where the problem is: val is either infinite or NaN.

So... val is being set to let val = Float64(sender.value) * totalsecs, and totalsecs is set by the duration let totalsecs = CMTimeGetSeconds(duration) ...

And... you say duration is NaN.

Find out why duration is NaN, and you have your answer.

Upvotes: 5

Related Questions