AVplayer
An
AVPlayer
is a controller object used to manage the playback and timing of a media asset. It provides the interface to control the player’s transport behavior such as its ability to play, pause, change the playback rate, and seek to various points in time within the media’s timeline. You can use an AVPlayer
to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming.
AVPlayer是一个控制对象用于管理媒体asset的播放,它提供了相关的接口控制播放器的行为,比如:播放、暂停、改变播放的速率、跳转到媒体时间轴上的某一个点(简单理解就是实现拖动功能显示对应的视频位置内容)。我们能够使用AVPlayer播放本地和远程的媒体文件(使用 HTTP Live Streaming),比如: QuickTime movies 和 MP3 audio files,所以AVPlayer可以满足音视频播放的基本需求。
注意:
1:AVPlayer继承NSObject,所以单独使用AVPlayer时无法显示视频的,必须将视频图层添加到AVPlayerLayer中方能显示视频。
2:AVPlayer一次只能播放单一的媒体资源(asset),但是player实例对象能够被重复用于播放其它媒体资源,可以调用
replaceCurrentItem(with:)方法更新当前播放资源。如果想播放多个资源,我们可以使用
AVPlayer的子类AVQueuePlayer,该类能够创建和管理多个媒体资源列队,
AVPlayer是一个动态对象,它的状态不断改变,有两种方式我们能够观察播放器的状态:
1:General State Observations: You can use Key-value observing (KVO) to observe state changes to many of the player’s dynamic properties, such as its
currentItem
or its playback rate
. You should register and unregister for KVO change notifications on the main thread. This avoids the possibility of receiving a partial notification if a change is being made on another thread. AVFoundation invokes observeValue(forKeyPath:of:change:context:)
on the main thread, even if the change operation is made on another thread.
基本状态观察者:你能够使用KVO来观察player动态属性的状态改变,比如像:
currentItem
或者它的播放速度。我们应该在主线程注册和去除KVO,这能够避免如果在其它线程发送改变而导致接收局部通知,当发生通知,AVFoundation将在主线程调用observeValue(forKeyPath:of:change:context:)
方法,即使是在其他线程发生。
2:Timed State Observations: KVO works well for general state observations, but isn’t intended for observing continuously changing state like the player’s time.
AVPlayer
provides two methods to observe time changes:addPeriodicTimeObserver(forInterval:queue:using:)、
addBoundaryTimeObserver(forTimes:queue:using:)
These methods let you observe time changes either periodically or by boundary, respectively. As changes occur, the callback block or closure you supply to these methods is invoked giving you the opportunity to take some action such as updating the state of your player’s user interface.
时间状态观察者:KVO能够很好的观察生成的状态,但是并不能够观察播放时间的改变,所以AVPlayer提供了两个方法来观察时间的改变,
addPeriodicTimeObserver(forInterval:queue:using:)、
addBoundaryTimeObserver(forTimes:queue:using:),这两个方法能够让我们周期性或者边界方法进行观察,当改变发生,我们所提供的回调block或者闭包将会被触发,在闭包中我们将有机会更新播放器的状态和相关UI界面。
AVplayer和AVPlayerItem都是不可见对象,这因为意味着它们是不能够呈现视频在屏幕上,我们有两个基本方法在屏幕上显示视频:
1:AVKit: The best way to present your video content is by using the AVKit framework’s
AVPlayerViewController
class in iOS and tvOS or the AVPlayerView
class in macOS. These classes present the video content, along with playback controls and other media features giving you a full-featured playback experience.
使用AVKit,这是最好的方式呈现视频内容,我们只需要使用AVKit框架的AVPlayerViewController类,该类能够播放视频内容,并且带有相应的播放控件和一些其他的媒体特征,能够进行全屏播放。