AVplayer实现播放本地和网络视频(Swift3.0)

本文介绍了如何在Swift3.0中利用AVPlayer播放本地和网络视频,包括设置UI布局、观察资源准备、实现进度更新、监听playerItem状态以及处理播放控制事件。
摘要由CSDN通过智能技术生成
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类,该类能够播放视频内容,并且带有相应的播放控件和一些其他的媒体特征,能够进行全屏播放。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值