歸納筆記020:AVPlayer播放視頻

在開發中,單純使用AVPlayer類是無法顯示視頻的,要將視頻層添加至AVPlayerLayer中,這樣才能將視頻顯示出來,所以先在ViewController的@interface中添加以下屬性:

@property(nonatomic ,strong) AVPlayer *player;

@property(nonatomic ,strong) AVPlayerItem *playerItem;<br>@property (nonatomic,weak) IBOutletPlayerView *playerView;

其中playerView繼承自UIView,不過重寫了set和get方法,用於將player添加至playerView的AVPlayerLayer中,這樣才能順利將視頻顯示出來,使用步驟如下:

(1)      在線視頻鏈接存放在videoUrl中,然後初始化playerItem,playerItem是管理資源的對象。

(2)      監聽playerItem的status和loadedTimeRange屬性。

(3)      當status等於AVPlayerStatusReadyToPlay是代表視頻已經可以播放了,調用play方法播放。

(4)      loadedTimeRange屬性代表已經緩衝的進度,監聽此屬性可以在UI中更新緩衝進度。

(5)      添加一個通知,用於監聽視頻是否已經播放完畢。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章