2

When I am trying to play the video I am getting this exception below.

*** Assertion failure in -[CustomSlider _setValue:minValue:maxValue:andSendAction:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.119.2/UISlider.m:1477

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to set a slider's minimumValue (0.000000) to be larger than the maximumValue (nan)'

libc++abi.dylib: terminating with uncaught exception of type NSException

This is my code:

This piece of code is working fine when I run on the iOS 13 devices but getting crash when I run this on the iOS 12.3.1 device.

SDK: iOS 13
Tool: XCode 11.3.1
Target Device: iPhone 6S Plus running iOS 12.3.1

func updateUIforPlayerItemStatus() {
    guard let currentItem = player.currentItem else { return }
    
    switch currentItem.status {        
    case .readyToPlay:
        playPauseButton.isEnabled = true
        guard player.currentItem!.duration >= CMTime.zero else {
            return
        }
        
        let newDurationSeconds = Float(currentItem.duration.seconds)
        
        let currentTime = Float(player.currentTime().seconds)
        timeSlider.minimumValue = 0.0
        timeSlider.maximumValue = newDurationSeconds
        timeSlider.value = currentTime
        timeSlider.isEnabled = true
        durationLabel.isEnabled = true
        durationLabel.text = createTimeString(time: newDurationSeconds)
        player.playImmediately(atRate: playbackRate)            
        
    default:
        playPauseButton.isEnabled = false
        timeSlider.isEnabled = false
        durationLabel.isEnabled = false
    }
}
Community
  • 1
  • 1
s4nj33b
  • 31
  • 3

3 Answers3

3

The error message says that maximumValue is NaN (not-a-number), which means newDurationSeconds is not a number, which means currentItem.duration.seconds is not a number. Check to make sure it's not NaN before using it, like this:

[...]
guard currentItem.duration >= .zero, !currentItem.duration.seconds.isNaN else {
    return
}

let newDurationSeconds = Float(currentItem.duration.seconds)

[...]
TylerP
  • 9,600
  • 4
  • 39
  • 43
2

The slider's maximum value is nan which means the duration of your avPlayer is indefinite, you can get an estimate using:

    let newDurationSeconds = Float(currentItem.asset.duration.seconds ?? 00)
Celeste
  • 1,519
  • 6
  • 19
1

This error comes when duration is to be .Nan, Use this code to fix it

        if let duration = musicPlayer.instance.player.currentItem?.duration {
            guard duration.isNaN else {
                return
            }
            let seconds : Float64 = duration
        self.sliderAudio.maximumValue = Float(seconds)
        }
Vikas Rajput
  • 1,754
  • 1
  • 14
  • 26