iOS AVAsset.duration is zero for HTTP live streaming but works for progressive

前端 未结 1 667
孤街浪徒
孤街浪徒 2021-02-20 16:48

I have an iOS app that plays video from a HTTP Live stream \"playlist.m3u8\" and I have a custom player created using AVPlayer. To handle normal user interactions such as scrub

相关标签:
1条回答
  • 2021-02-20 17:03

    https://developer.apple.com/library/ios/releasenotes/AudioVideo/RN-AVFoundation-Old/#//apple_ref/doc/uid/TP40011199-CH1-SW4

    The docs above mention that duration should now be obtained from the AVPlayerItem instance, rather than its corresponding AVAsset. To get the duration from the current player item via key-value observing, I use the following method (originally pulled from NGMoviePlayer which was written for iOS 4.0):

    - (void)loadPlayerWithItem:(AVPlayerItem *)playerItem {
        self.player = [AVPlayer playerWithPlayerItem:playerItem];
        ...
        // changed this from previous value currentItem.asset.duration
        [self.player addObserver:self forKeyPath:@"currentItem.duration"
                                         options:0
                                         context:nil];
        ...
    }
    

    I implemented the above change in my player and the duration is working now! This change in AVFoundation was the root cause of the issue. CMTimeFlags = 17 indicates kCMTimeFlags_Indefinite & kCMTimeFlags_Valid, and the docs specify:

    In particular, the duration reported by the URL asset for streaming-based media is typically kCMTimeIndefinite, while the duration of a corresponding AVPlayerItem may be different and may change while it plays.

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