如何监听 AVPlayer "on seek" 事件?
How to listen AVPlayer "on seek" events?
我正在寻找 AVPlayer/AVPlayerItem/AVAsset
的任何 "seek" 相关事件
有什么办法可以监听这样的事件吗? (也许是 kvo-listeners 的一些关键,不确定)
似乎有 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()
}
简单的解决方案就是在需要时检查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
我正在寻找 AVPlayer/AVPlayerItem/AVAsset
的任何 "seek" 相关事件有什么办法可以监听这样的事件吗? (也许是 kvo-listeners 的一些关键,不确定)
似乎有 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()
}
简单的解决方案就是在需要时检查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