【问题标题】:How to listen AVPlayer "on seek" events?如何收听 AVPlayer “on seek” 事件?
【发布时间】:2016-06-16 10:05:52
【问题描述】:

我正在为 AVPlayer/AVPlayerItem/AVAsset 寻找任何与“搜索”相关的事件

有什么方法可以监听此类事件吗? (也许是 kvo-listeners 的一些关键,不确定)

【问题讨论】:

    标签: ios avplayer avasset avplayeritem


    【解决方案1】:

    KVO 似乎有一个解决方法(如果是错误的决定,请告诉我):

    case "currentItem.playbackBufferEmpty":
        if (player.rate == 0) {
            state = "SEEKING"
            on_seeking()
        }
    case "currentItem.status":
        if (player.currentItem?.status == .ReadyToPlay && player.rate == 0) {
            if (state != "SEEKING") {
                on_seeking()
            }
    
            state = "SEEKED"
            on_seeked()
        }
    

    【讨论】:

    • 对我来说效果很好(主要在 HLS 视频上尝试,不知道它是否与其他选项不同)。
    • 我会试试的。谢谢!
    • 你如何用目标 c 做到这一点。
    • @norlin 我的 SEEKING 和 SEEKED 被同时调用,即使我没有以 seek 结束
    • @Saty 是的,如果没有捕获到单独的“SEEKING”事件,请查看代码。我不知道如何更好地解决它......
    【解决方案2】:

    简单的解决方案就是在需要时检查 AVPlayer 属性的 rate。 查看文档:https://developer.apple.com/documentation/avfoundation/avplayer/1388846-rate

    extension AVPlayer {
    
    
    /*!
     @property      rate
     @abstract      Indicates the desired rate of playback; 0.0 means "paused", 1.0 indicates a desire to play at the natural rate of the current item.
     @discussion
     Setting the value of rate to 0.0 pauses playback, causing the value of timeControlStatus to change to AVPlayerTimeControlStatusPaused.
     Setting the rate to a non-zero value causes the value of timeControlStatus to become either AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate or AVPlayerTimeControlStatusPlaying, depending on whether sufficient media data has been buffered for playback to occur and whether the player's default behavior of waiting in order to minimize stalling is permitted. See discussion of AVPlayerTimeControlStatus for more details.
    
     AVPlayer can reset the desired rate to 0.0 when a change in overall state requires playback to be halted, such as when an interruption occurs on iOS, as announced by AVAudioSession, or when the playback buffer becomes empty and playback stalls while automaticallyWaitsToMinimizeStalling is NO.
    
     The effective rate of playback may differ from the desired rate even while timeControlStatus is AVPlayerTimeControlStatusPlaying, if the processing algorithm in use for managing audio pitch requires quantization of playback rate. For information about quantization of rates for audio processing, see AVAudioProcessingSettings.h. You can always obtain the effective rate of playback from the currentItem's timebase; see the timebase property of AVPlayerItem.
     */
    open var rate: Float
    

    【讨论】:

    • 它将如何帮助检测搜索事件?
    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 2022-01-14
    • 1970-01-01
    • 2022-08-16
    • 2015-08-06
    • 2015-12-10
    • 2013-05-06
    • 2015-04-16
    相关资源
    最近更新 更多