美文网首页
AVPlayerTimeControlStatus总结

AVPlayerTimeControlStatus总结

作者: 左方 | 来源:发表于2020-12-02 13:55 被阅读0次

在iOS10以后,可用AVPlayer的AVPlayerTimeControlStatus来判断播放状态。
官方文档介绍如下:

一、AVPlayerTimeControlStatus

@enum AVPlayerTimeControlStatus
@abstract
These constants are the allowable values of AVPlayer's timeControlStatus property. This discussion pertains when automaticallyWaitsToMinimizeStalling is YES, the default setting, and exceptions are discussed in connection with automaticallyWaitsToMinimizeStalling.

此属性适用于automaticallyWaitsToMinimizeStalling = YES的时候。

1. AVPlayerTimeControlStatusPaused

This state is entered upon receipt of a -pause message, an invocation of -setRate: with a value of 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.
In this state, playback is paused indefinitely and will not resume until 1) a subsequent -play message is received or 2) a -setRate: or -playImmediatelyAtRate: message with a non-zero value for rate is received and sufficient media data has been buffered for playback to proceed.
当接收到-pause消息(调用-setRate速率值为0)时,会进入此状态,当整体状态的变化需要暂停播放时(例如在AVAudioSession宣布的iOS上发生中断时)。
在这种状态下,播放将无限期暂停,直到1)接收到后续的-play消息或2)接收到-setRate:或-playImmediatelyAtRate:消息,该消息的rate值非零且已接收到足够的媒体数据后,播放才会无限期暂停 缓冲以继续播放。

2. AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate

This state is entered when 1) the playback buffer becomes empty and playback stalls in AVPlayerTimeControlStatusPlaying, 2) when rate is set from zero to non-zero in AVPlayerTimeControlStatusPaused and insufficient media data has been buffered for playback to occur, or 3) when the player has no item to play, i.e. when the receiver's currentItem is nil.
In this state, the value of the rate property is not currently effective but instead indicates the rate at which playback will start or resume. Refer to the value of reasonForWaitingToPlay for details about why the receiver is waiting and the conditions that allow waitStatus to change to AVPlayerWaitStatusPlaying.
While waiting for buffering, you can attempt to start playback of any available media data via -playImmediatelyAtRate:.

1)AVPlayerTimeControlStatus为Playing时,播放缓冲区变空,并且停止播放时,进入此状态;
2)AVPlayerTimeControlStatus为Paused时,开始播放,并且没有足够的媒体缓冲数据以进行播放时;
3)没有要播放的项目,即当接收者的currentItem为nil时。
在此状态下,rate速率属性的值无效,而是表示start或resume的速率。
有关接收方为何等待以及允许waitStatus更改为AVPlayerWaitStatusPlaying的条件的详细信息,请参考reasonForWaitingToPlay的值。
在等待缓冲时,可以尝试通过-playImmediatelyAtRate:开始播放任何可用的媒体数据。

3. AVPlayerTimeControlStatusPlaying

In this state, playback is currently progressing and rate changes will take effect immediately. Should playback stall because of insufficient media data, timeControlStatus will change to AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate.
此状态为播放正在进行中,在此状态下速率更改将立即生效。 如果由于媒体缓冲数据不足而导致播放停止,则timeControlStatus将更改为AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate。

二、automaticallyWaitsToMinimizeStalling

When this property is YES, whenever 1) the rate is set from zero to non-zero or 2) the playback buffer becomes empty and playback stalls, the player will attempt to determine if, at the specified rate, its currentItem will play to the end without interruptions. Should it determine that such interruptions would occur and these interruptions can be avoided by delaying the start or resumption of playback, the value of timeControlStatus will become AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate and playback will start automatically when the likelihood of stalling has been minimized.
当此属性为YES时,每当
1)速率从零设置为非零或
2)播放缓冲区变空并且播放停止时
播放器将尝试以指定的速率将其currentItem播放到结束而不会中断。如果它确定会发生此类中断,并且可以通过延迟回放的开始或恢复来避免这些中断,则timeControlStatus的值将变为WaitingToPlayAtSpecifiedRate,并且在将停顿的可能性降到最低后,回放将自动开始。

这段的意思是:当automaticallyWaitsToMinimizeStalling = YES;
当开始播放时(或者缓存都播完了时),播放器会等待缓存足够多了,才开始播放。这时timeControlStatus将变为WaitingToPlayAtSpecifiedRate

You may want to set this property to NO when you need precise control over playback start times, e.g., when synchronizing multiple instances of AVPlayer, and you should set it to NO if you use an AVAssetResourceLoader delegate to load media data (more on this below). If the value of this property is NO, reasonForWaitingToPlay cannot assume a value of AVPlayerWaitingToMinimizeStallsReason.
This implies that setting rate to a non-zero value in AVPlayerTimeControlStatusPaused will cause playback to start immediately as long as the playback buffer is not empty. When the playback buffer becomes empty during AVPlayerTimeControlStatusPlaying and playback stalls, playback state will switch to AVPlayerTimeControlStatusPaused and the rate will become 0.0.
当您需要精确控制播放开始时间时,例如,当同步AVPlayer的多个实例时,可能需要将此属性设置为NO,并且如果您使用AVAssetResourceLoader委托加载媒体数据,则应将其设置为NO。 )。如果此属性的值为NO,则reasonForWaitingToPlay不能假定值为AVPlayerWaitingToMinimizeStallsReason。
这意味着只要播放缓冲区不为空,在AVPlayerTimeControlStatusPaused中将rate设置为非零值将导致播放立即开始。当在AVPlayerTimeControlStatusPlaying期间播放缓冲区变空并且播放停止时,播放状态将切换为AVPlayerTimeControlStatusPaused,并且速率将变为0.0。

这段的意思是:如果使用AVAssetResourceLoader加载数据。该属性最好设为NO。
如果automaticallyWaitsToMinimizeStalling = NO;时,reasonForWaitingToPlay不会变为AVPlayerWaitingToMinimizeStallsReason(AVPlayerTimeControlStatus为WaitingToPlayAtSpecifiedRate时,此属性描述播放器当前等待的原因)
当AVPlayerTimeControlStatus = Paused时,只要有缓存,改变速率就可以直接播放(不用等待播放器自己判断缓存够不够);
当AVPlayerTimeControlStatus为Playing时,缓存不够会切换到Paused(不会切到WaitingToPlayAtSpecifiedRate)

Changing the value of this property to NO while the value of timeControlStatus is AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate with a reasonForWaitingToPlay of AVPlayerWaitingToMinimizeStallsReason will cause the player to attempt playback at the specified rate immediately.
当timeControlStatus的值为AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate并且AVPlayerWaitingToMinimizeStallsReason的reasonForWaitingToPlay时将此属性的值更改为NO,将导致播放器立即尝试以指定的速率播放。

这段的意思是:AVPlayerTimeControlStatus为WaitingToPlayAtSpecifiedRate,并且reasonForWaitingToPlay的原因为AVPlayerWaitingToMinimizeStallsReason时,设置automaticallyWaitsToMinimizeStalling = NO,将立即播放

For clients linked against iOS 10.0 and running on that version or later or linked against OS X 10.12 and running on that version or later, the default value of this property is YES.
In versions of iOS prior to iOS 10.0 and versions of OS X prior to 10.12, this property is unavailable, and the behavior of the AVPlayer corresponds to the type of content being played. For streaming content, including HTTP Live Streaming, the AVPlayer acts as if automaticallyWaitsToMinimizeStalling is YES. For file-based content, including file-based content accessed via progressive http download, the AVPlayer acts as if automaticallyWaitsToMinimizeStalling is NO.
If you employ an AVAssetResourceLoader delegate that loads media data for playback, you should set the value of your AVPlayer’s automaticallyWaitsToMinimizeStalling property to NO. Allowing the value of automaticallyWaitsToMinimizeStalling to remain YES when an AVAssetResourceLoader delegate is used for the loading of media data can result in poor start-up times for playback and poor recovery from stalls, because the behaviors provided by AVPlayer when automaticallyWaitsToMinimizeStalling has a value of YES depend on predictions of the future availability of media data that that do not function as expected when data is loaded via a client-controlled means, using the AVAssetResourceLoader delegate interface.
You can allow the value of automaticallyWaitsToMinimizeStalling to remain YES if you use an AVAssetResourceLoader delegate to manage content keys for FairPlay Streaming, to provide dynamically-generated master playlists for HTTP Live Streaming, or to respond to authentication challenges, but not to load media data for playback.
对于链接到iOS 10.0并在该版本或更高版本上运行的客户端,或链接到OS X 10.12并在该版本或更高版本上运行的客户端,此属性的默认值为YES。
在iOS 10.0之前的iOS版本和10.12之前的OS X版本中,此属性不可用,并且AVPlayer的行为与正在播放的内容类型相对应。对于包括HTTP Live Streaming在内的流内容,AVPlayer的行为就像autoWaitsToMinimizeStalling是YES一样。对于基于文件的内容,包括通过渐进式http下载访问的基于文件的内容,AVPlayer的行为就像autoWaitsToMinimizeStalling为NO一样。
如果您使用AVAssetResourceLoader委托来加载要播放的媒体数据,则应将AVPlayer的automaticWaitsToMinimizeStalling属性的值设置为NO。当使用AVAssetResourceLoader委托来加载媒体数据时,允许autoWaitsToMinimizeStalling的值保持为YES可能会导致启动时间较差,并且无法从停顿中恢复,因为AVPlayer在autoWaitsToMinimizeStalling值为YES时提供的行为取决于关于使用AVAssetResourceLoader委托接口通过客户端控制的方式加载数据时无法正常运行的媒体数据未来可用性的预测。
如果您使用AVAssetResourceLoader委托来管理FairPlay Streaming的内容密钥,为HTTP Live Streaming提供动态生成的主播放列表或响应身份验证挑战,但不为以下内容加载媒体数据,则可以允许automaticWaitsToMinimizeStalling的值保持为YES。回放。

这段的意思是:
iOS 10以后系统,该属性默认为YES。
iOS 10以前系统,HTTP Live Streaming这类的流媒体,就好像该属性设为YES一样。
播放下载的文件,就好像该属性设为NO一样。
当使用AVAssetResourceLoader加载数据时,如果autoWaitsToMinimizeStalling为YES会导致启动时间过长。缓存播放完,恢复到播放的时间过长。

三、reasonForWaitingToPlay

When the value of timeControlStatus is AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate, this property describes why the player is currently waiting. It is nil otherwise.
You can use the value of reasonForWaitingToPlay to show UI indicating the player's waiting state conditionally.
This property is key value observable.
Possible values are AVPlayerWaitingWithNoItemToPlayReason, AVPlayerWaitingWhileEvaluatingBufferingRateReason, and AVPlayerWaitingToMinimizeStallsReason.
当timeControlStatus的值为WaitingToPlayAtSpecifiedRate时,此属性描述播放器当前等待的原因。 否则为nil。
您可以使用reasonForWaitingToPlay的值来显示UI,以有条件地指示玩家的等待状态。
此属性是可观察到的键值。
可能的值为AVPlayerWaitingWithNoItemToPlayReason,AVPlayerWaitingWhileEvaluatingBufferingRateReason和AVPlayerWaitingToMinimizeStallsReason。

相关文章

网友评论

      本文标题:AVPlayerTimeControlStatus总结

      本文链接:https://www.haomeiwen.com/subject/ipxziktx.html