有没有办法用AVPlayer区分直播流和点播文件流?

发布于 2024-11-23 15:43:49 字数 231 浏览 2 评论 0原文

我正在尝试为多种类型的流媒体创建一个更通用的媒体控制器,并希望使 UI 适应流的类型;

  • 当它是点播文件流(即正在流式传输的单个 MP3 文件)时,您应该能够向前和向后搜索。因此,搜索滑块应该是可见的。
  • 当它是直播时,不可能向前和向后搜索,因此应该隐藏搜索滑块。

有没有办法从 AVPlayer(或者可能是 AVPlayerItem 或 AVAsset)确定流的类型是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜宝宝 2024-11-30 15:43:49

直播时长不定

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

仅当AVPlayerItem项状态为AVPlayerItemStatusReadyToPlay时才需要检查时长。

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.

梦纸 2024-11-30 15:43:49

对于那些仍在寻找此功能的人,

AVPlayerItem > AVPlayerItemAccessLogEvent > playbackType 属性可能会有所帮助。
我已经检查过“VOD”、“LIVE”类型是否已正确返回。

更多详细信息请参见此处

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

杀お生予夺 2024-11-30 15:43:49

解决方案

您可以使用此代码轻松检测播放类型:

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")")
        }

将观察者代码添加到您通常开始收听它们的位置。

不要忘记

另外,不要忘记在 deinit 中删除观察者;)

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

希望这会对某人有所帮助:)

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

想挽留 2024-11-30 15:43:49

看来这是不可能的。

然而,人们可以检查直播的持续时间,该持续时间似乎始终高于 33000 秒。但是,该值仍然会波动,并且不希望对此进行检查,因为它可能会导致意外的行为。

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.

月棠 2024-11-30 15:43:49
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 {
      
    }
})

播放类型可以是直播、VOD 或文件。如果返回 nil,则播放类型未知。更多详细信息请参见此处

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文