Home > OS >  How I can get duration of AVPlayer?
How I can get duration of AVPlayer?

Time:09-20

I want to make slider for my AVPlayer, I used AVPlayer because I want to stream a mp3 file from Firebase..

I tried:

var duration = player.currentItem.asset.duration

but that obviously doesn't work :)

I read the documentation of AVPlayer but I don't get it There is seek function and I don't know how that work In AVAudioPlayer there is duration function which show that information, but in AVPlayer there is nothing like unfortunately...

CodePudding user response:

Hope this will help you.

player.currentTime

CodePudding user response:

I solved this.

I used currentTime in previous answer to get currentTime and convert that into seconds using CMTimeGetSeconds function.

But for get complete duration I just made a function getDuration:

func getDuration() -> String {
    
    var duration = audioManager.player?.currentItem?.asset.duration
    var getDurationString = DateComponentsFormatter.positional.string(from:CMTimeGetSeconds(duration!))
    
    return getDurationString!
}

and just call that function into my Text view.

  • Related