How to get Duration from AVPlayer (Not AVAudioPlayer)?

前端 未结 7 1090
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 03:43

I would like to make a UISlider(scrubber) for my AVPlayer. But since this is not an AVAudioPlayer, it doesn\'t have a built in duration. Any suggestion on how to create the

相关标签:
7条回答
  • 2020-12-13 04:17

    For Swift to get duration in seconds

    if let duration = player.currentItem?.asset.duration {
        let seconds = CMTimeGetSeconds(duration)
        print("Seconds :: \(seconds)")
    }
    
    0 讨论(0)
  • 2020-12-13 04:27

    As of iOS 4.3, you can use the slightly shorter:

    self.player.currentItem.duration;
    
    0 讨论(0)
  • 2020-12-13 04:28
    self.player.currentItem.asset.duration
    

    Got it!

    0 讨论(0)
  • 2020-12-13 04:29

    Noted from StitchedStreamPlayer

    You should use player.currentItem.duration

    - (CMTime)playerItemDuration
    {
        AVPlayerItem *thePlayerItem = [player currentItem];
        if (thePlayerItem.status == AVPlayerItemStatusReadyToPlay)
        {        
            /* 
             NOTE:
             Because of the dynamic nature of HTTP Live Streaming Media, the best practice 
             for obtaining the duration of an AVPlayerItem object has changed in iOS 4.3. 
             Prior to iOS 4.3, you would obtain the duration of a player item by fetching 
             the value of the duration property of its associated AVAsset object. However, 
             note that for HTTP Live Streaming Media the duration of a player item during 
             any particular playback session may differ from the duration of its asset. For 
             this reason a new key-value observable duration property has been defined on 
             AVPlayerItem.
    
             See the AV Foundation Release Notes for iOS 4.3 for more information.
             */     
    
            return([playerItem duration]);
        }
    
        return(kCMTimeInvalid);
    }
    
    0 讨论(0)
  • 2020-12-13 04:34

    Swift 5

    Put this code inside some function that you desired :

    let duration = player.currentItem?.duration.seconds ?? 0
    let playDuration = formatTime(seconds: duration) //Duration RESULT
    

    Create a function called: formatTime(seconds: Double)

    func formatTime(seconds: Double) -> String {
        let result = timeDivider(seconds: seconds)
        let hoursString = "\(result.hours)"
        var minutesString = "\(result.minutes)"
        var secondsString = "\(result.seconds)"
    
        if minutesString.count == 1 {
            minutesString = "0\(result.minutes)"
        }
        if secondsString.count == 1 {
            secondsString = "0\(result.seconds)"
        }
    
        var time = "\(hoursString):"
        if result.hours >= 1 {
            time.append("\(minutesString):\(secondsString)")
        }
        else {
            time = "\(minutesString):\(secondsString)"
        }
        return time
    }
    

    Then, another function to translate seconds to hour, minute and second. Since the seconds that by layerLayer.player?.currentItem?.duration.seconds will have long Double, so this is needed to become Human Readable.

    func timeDivider(seconds: Double) -> (hours: Int, minutes: Int, seconds: Int) {
        guard !(seconds.isNaN || seconds.isInfinite) else {
            return (0,0,0)
        }
        let secs: Int = Int(seconds)
        let hours = secs / 3600
        let minutes = (secs % 3600) / 60
        let seconds = (secs % 3600) % 60
        return (hours, minutes, seconds)
    }
    

    Hope it's complete your answer.

    0 讨论(0)
  • 2020-12-13 04:36

    headers

    #import <AVFoundation/AVPlayer.h>
    #import <AVFoundation/AVPlayerItem.h>
    #import <AVFoundation/AVAsset.h>
    

    code

    CMTime duration = self.player.currentItem.asset.duration;
    float seconds = CMTimeGetSeconds(duration);
    NSLog(@"duration: %.2f", seconds);
    

    frameworks

    AVFoundation
    CoreMedia
    
    0 讨论(0)
提交回复
热议问题