简体   繁体   中英

Is there a way to distinguish between a live stream and an on-demand file stream with AVPlayer?

I'm trying to create a more generic media controller for several types of streaming media and want to adapt the UI to the type of stream;

  • When it's an on-demand file stream (ie a single MP3 file that's being streamed), you should be able to seek forward and backward. Thus, the seek slider should be visible.
  • When it's a live stream, it isn't possible to seek forward and backward, and thus the seek slider should be hidden.

Is there any way to determine from the AVPlayer (or perhaps the AVPlayerItem or AVAsset) what the type of stream is?

The duration of live video is indefinite :

AVPlayer * player = ...;
const BOOL isLive = CMTIME_IS_INDEFINITE([player currentItem].duration);

You have to check the duration only when the AVPlayerItem item status is AVPlayerItemStatusReadyToPlay .

For those who are still looking for this feature,

AVPlayerItem > AVPlayerItemAccessLogEvent > playbackType property might be helpful. I already checked "VOD", "LIVE" types were appropriately returned from it.

more detail in here

It appears that this is not possible.

However, one could check the duration of a live stream, which seems to be consistently above 33000 seconds. However, this value still fluctuates and checking for this is undesirable, since it might cause unexpected behavior.

Solution

You can use this code to easily detect the playback type:

NotificationCenter.default.addObserver(
            forName: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
            object: nil,
            queue: OperationQueue.main) { [weak self] (notification) in
                guard let self = self else { return }

                guard let playerItem = notification.object as? AVPlayerItem,
                    let lastEvent = playerItem.accessLog()?.events.last else {
                    return
                }

                // Here you can set the type (LIVE | VOD | FILE or unknow if it's a nil):
                print("Playback Type: \(lastEvent.playbackType ?? "NA")")
        }

Add the observer code to where you normally start to listen to them.

Also, don't forget to remove the observer at the deinit;)

deinit {
    NotificationCenter.default.removeObserver(self,
                           name: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
                         object: self)
}

Hope this will help someone:)

player?.addPeriodicTimeObserver(forInterval: interval, queue: .main, using: { time in
    let playbackType = self.player?.currentItem?.accessLog()?.events.last?.playbackType!
    print("Playback Type: \(lastEvent.playbackType ?? "NA")")
    if playbackType == StreamingType.Live.rawValue {
    
    }
    else if playbackType == StreamingType.Vod.rawValue {
      
    }
})

The playback type can be live, VOD, or from a file. If nil is returned the playback type is unknown. more detail in here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM