有一個月沒寫部落格了, 今天分享一篇關於多媒體的文章, 平時我多分享一些UI方面的零散知識, 這種一般好理解, 讓人願意學習, 看完能夠實現某些效果, 今天也是本著這個目的, 今天通過簡單封裝一個視訊播放器, 來學一些AVPlayer相關的一些知識. 在這裡的程式碼格式似乎閱讀不是很理想, 我試著做了修改, 並沒有修改成功, 可以直接下載Demo, 或者點選我留下的備用連結(在文末).
播放器地址https://github.com/summerxx27/XXVideoPlayer
- 編譯環境 Xcode 8
- AVPlayer相關
- AVPlayerItem
- 支援多種視訊格式
- 完成了 快進 快退 暫停 播放 全屏等視訊播放器的基礎功能
- 後期會新增更加豐富的內容, 還可能寫一個Swift版本的
1 |
_avPlayerItem = [[AVPlayerItem alloc] initWithURL:playFormURL]; |
1 2 |
// 通過AVPlayerItem建立一個AVPlayer物件 self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:_avPlayerItem]; |
1 2 3 4 |
<span class="hljs-comment">// 建立 AVPlayerLayer <span class="hljs-keyword">self</span><span class="hljs-variable">.playLayer</span> = [<span class="hljs-built_in">AVPlayerLayer</span> playerLayerWithPlayer:<span class="hljs-keyword">self</span><span class="hljs-variable">.avPlayer</span>]; // 新增到要顯示視訊的View</span> [<span class="hljs-keyword">self</span><span class="hljs-variable">.view</span><span class="hljs-variable">.layer</span> addSublayer:<span class="hljs-keyword">self</span><span class="hljs-variable">.playLayer</span>]; |
1 2 3 4 5 6 7 8 9 |
<span class="hljs-comment">// 播放 | 暫停</span> - (<span class="hljs-keyword">void</span>)xx_VideoPlay { [<span class="hljs-keyword">self</span><span class="hljs-variable">.avPlayer</span> play]; } - (<span class="hljs-keyword">void</span>)xx_VideoPause { [<span class="hljs-keyword">self</span><span class="hljs-variable">.avPlayer</span> pause]; } |
1 2 |
<span class="hljs-comment">// 快進處理 slider 就是滑動條.</span> [<span class="hljs-keyword">self</span><span class="hljs-variable">.avPlayer</span> seekToTime:CMTimeMakeWithSeconds(slider<span class="hljs-variable">.value</span>, <span class="hljs-keyword">self</span><span class="hljs-variable">.avPlayerItem</span><span class="hljs-variable">.currentTime</span><span class="hljs-variable">.timescale</span>)]; |
1 2 |
<span class="hljs-comment">// 獲取總的播放時間 self.avPlayerItem(當前播放AVPlayerItem的物件)</span> CMTimeGetSeconds(<span class="hljs-keyword">self</span><span class="hljs-variable">.avPlayerItem</span><span class="hljs-variable">.asset</span><span class="hljs-variable">.duration</span>) |
1 2 3 4 5 6 7 8 9 10 |
<span class="hljs-comment">// 觀察AVPlayerItem的播放狀態</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemTimeJumpedNotification</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_7, <span class="hljs-number">5</span>_0); <span class="hljs-comment">// the item's current time has changed discontinuously</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemDidPlayToEndTimeNotification</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_7, <span class="hljs-number">4</span>_0); <span class="hljs-comment">// item has played to its end time</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemFailedToPlayToEndTimeNotification</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_7, <span class="hljs-number">4</span>_3); <span class="hljs-comment">// item has failed to play to its end time</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemPlaybackStalledNotification</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_9, <span class="hljs-number">6</span>_0); <span class="hljs-comment">// media did not arrive in time to continue playback</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemNewAccessLogEntryNotification</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_9, <span class="hljs-number">6</span>_0); <span class="hljs-comment">// a new access log entry has been added</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemNewErrorLogEntryNotification</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_9, <span class="hljs-number">6</span>_0); <span class="hljs-comment">// a new error log entry has been added</span> <span class="hljs-comment">// notification userInfo key type</span> <span class="hljs-built_in">AVF_EXPORT</span> <span class="hljs-built_in">NSString</span> *<span class="hljs-keyword">const</span> <span class="hljs-built_in">AVPlayerItemFailedToPlayToEndTimeErrorKey</span> <span class="hljs-built_in">NS_AVAILABLE</span>(<span class="hljs-number">10</span>_7, <span class="hljs-number">4</span>_3); <span class="hljs-comment">// NSError</span> |
1 2 3 4 |
<span class="hljs-comment">// 旋轉方法 之後 調整frame</span> [<span class="hljs-keyword">self</span><span class="hljs-variable">.view</span> setTransform:<span class="hljs-built_in">CGAffineTransformMakeRotation</span>(M_PI_2)]; <span class="hljs-comment">// 取消全屏</span> [<span class="hljs-keyword">self</span><span class="hljs-variable">.view</span> setTransform:<span class="hljs-built_in">CGAffineTransformIdentity</span>]; |
1 2 3 4 |
<span class="hljs-comment">// 隱藏狀態列的相關方法</span> [[<span class="hljs-built_in">UIApplication</span> sharedApplication] setStatusBarHidden:<span class="hljs-literal">YES</span> withAnimation:<span class="hljs-built_in">UIStatusBarAnimationFade</span>]; <span class="hljs-comment">// 同時需要在info.plist檔案中進行配置</span> <span class="hljs-comment">// View controller-based status bar appearance = NO</span> |
以上就是對於視訊播放器封裝用到的知識點了~
第一次學習AVPlayer 難免出現錯誤的地方, 請告知我, 這樣我就能及時修正了
感謝 36Kr 的開源, 讓我能夠學習相關的知識點
不同的是 36Kr使用MPMoviePlayerController, 而我使用AVPlayer, UI方面我直接使用了他提供的UI方便快速, 當然可以隨意修改, 簡單方便.
最後感謝您的閱讀, 如果感覺有幫助可以關注我 和我一起學習!
http://www.jianshu.com/p/9aa30f00e13b
我是夏天, 暖暖的夏天
End
打賞支援我寫出更多好文章,謝謝!
打賞作者
打賞支援我寫出更多好文章,謝謝!
任選一種支付方式