Reputation: 2279
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;
Is there any way to determine from the AVPlayer (or perhaps the AVPlayerItem or AVAsset) what the type of stream is?
Upvotes: 6
Views: 2533
Reputation: 8513
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 :)
Upvotes: 2
Reputation: 1
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
Upvotes: 0
Reputation: 53
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
Upvotes: 2
Reputation: 151
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
.
Upvotes: 8
Reputation: 2279
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.
Upvotes: 1