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

5

6

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 (i.e. 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?

Proposal answered 12/7, 2011 at 15:10 Comment(1)
Hmm... so far looking at the duration gives some decent results. Streams all seem to have 33k+ seconds in them. But I'm not sure I want to rely on that.Proposal
P
1

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.

Proposal answered 15/9, 2011 at 14:55 Comment(1)
Good news, it's possible :)Pfaff
C
8

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.

Chamblee answered 23/12, 2016 at 0:0 Comment(1)
for swift let isLiveStream = player?.currentItem?.duration.isIndefiniteCell
S
2

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

Sava answered 22/11, 2019 at 11:31 Comment(0)
P
2

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.

Don't Forget

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 :)

Pfaff answered 3/4, 2020 at 4:59 Comment(0)
P
1

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.

Proposal answered 15/9, 2011 at 14:55 Comment(1)
Good news, it's possible :)Pfaff
M
0
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

Metheglin answered 16/5, 2022 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.