How to get Duration from AVPlayer (Not AVAudioPlayer)?

前端 未结 7 1133
隐瞒了意图╮
隐瞒了意图╮ 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: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);
    }
    

提交回复
热议问题