淺析iOS中視頻播放的幾種方案
1、AVPlayer
(1) 優(yōu)缺點(diǎn)
優(yōu)點(diǎn):可以自定義 UI, 進(jìn)行控制
缺點(diǎn):單純的播放,沒有控制 UI(進(jìn)度,暫停,播放等按鈕),而且如果要顯示播放界面, 需要借助AVPlayerLayer, 添加圖層到需要展示的圖層上
(2)實(shí)現(xiàn)遠(yuǎn)程視頻播放
實(shí)現(xiàn)播放功能(只有聲音)
1.導(dǎo)入框架
#import <AVFoundation/AVFoundation.h>
2.通過(guò)遠(yuǎn)程 URL 創(chuàng)建 AVPlayer 對(duì)象
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; _player = [AVPlayer playerWithURL:remoteURL];
3.開始播放
[self.player play];
存在問(wèn)題:只能播放聲音, 看不到圖像
解決方案: 需要借助AVPlayerLayer對(duì)象, 根據(jù)player創(chuàng)建圖層, 添加到視圖上
顯示視頻
4.根據(jù) player 對(duì)象創(chuàng)建 AVPlayerLayer
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
5.設(shè)置 layer 的 frame
layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 16);
6.將layer 添加到需要展示的 View 的 layer 上
[self.view.layer addSublayer:layer];
2、MPMoviePlayerController
(1)優(yōu)缺點(diǎn)
優(yōu)點(diǎn):自帶的播放控制UI(進(jìn)度條,上一個(gè),下一個(gè),暫停), 不需要手動(dòng)添加
缺點(diǎn):不能自定義UI
只能將此控制器視圖添加到其他視圖進(jìn)行展示
此控制器不是視圖控制器, 不能直接彈出
播放器的播放狀態(tài), 是通過(guò)通知的方式告訴外界
(2)視頻播放實(shí)現(xiàn)步驟
1.導(dǎo)入框架
#import <MediaPlayer/MediaPlayer.h>
2.根據(jù)URL, 創(chuàng)建控制器 MPMoviePlayerController
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:remoteURL];
3.設(shè)置播放視圖frame, 添加到需要展示的視圖上
// 設(shè)置播放視圖的frame self.moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 16); // 設(shè)置播放視圖控制樣式 self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen; // 添加播放視圖到要顯示的視圖 [self.view addSubview:self.moviePlayer.view];
4.播放
[self.moviePlayer play];
3、MPMoviePlayerViewController
(1)優(yōu)缺點(diǎn)
優(yōu)點(diǎn):自帶的播放控制UI, 不需要手動(dòng)添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動(dòng)調(diào)整視圖大小, 添加到其他視圖上
缺點(diǎn):不能自定義UI
(2)視頻播放實(shí)現(xiàn)步驟
1.導(dǎo)入框架
#import <MediaPlayer/MediaPlayer.h>
2.根據(jù)URL, 創(chuàng)建控制器 MPMoviePlayerViewController
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; _playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:remoteURL];
3.直接模態(tài)彈出該控制器(或者: 設(shè)置播放視圖frame, 添加到需要展示的視圖上)
[self presentViewController:self.playerVC animated:YES completion:^{ [self.playerVC.moviePlayer play]; }];
4.播放
[self.playerVC.moviePlayer play];
4、針對(duì)于第2種和第3種實(shí)現(xiàn)方案, 在iOS9.0之后, 統(tǒng)一使用AVPlayerViewController
(1)優(yōu)缺點(diǎn)
優(yōu)點(diǎn):自帶的播放控制UI, 不需要手動(dòng)添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動(dòng)調(diào)整視圖大小, 添加到其他視圖上
缺點(diǎn):不能自定義UI
(2)視頻播放實(shí)現(xiàn)步驟
1.導(dǎo)入框架
#import <AVFoundation/AVFoundation.h> #import <AVKit/AVKit.h>
2.根據(jù)URL創(chuàng)建AVPlayer
NSURL *remoteUrl = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]; AVPlayer *player = [AVPlayer playerWithURL:remoteUrl];
3.根據(jù)AVPlayer, 創(chuàng)建AVPlayerViewController控制器
_playerVc = [[AVPlayerViewController alloc] init]; _playerVc.player = player;
4.設(shè)置播放視圖frame, 添加到需要展示的視圖上
self.playerVc.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 16); [self.view addSubview:self.playerVc.view];
5.播放
[self.playerVc.player play];
步驟4和步驟5的合并:
[self presentViewController:self.playerVc animated:YES completion:nil];
總結(jié)
以上就是關(guān)于iOS中視頻播放的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者開發(fā)IOS能有所幫助。
- iOS開發(fā)之獲取系統(tǒng)相冊(cè)中的圖片與視頻教程(內(nèi)帶url轉(zhuǎn)換)
- IOS實(shí)現(xiàn)視頻動(dòng)畫效果的啟動(dòng)圖
- iOS實(shí)現(xiàn)視頻和圖片的上傳思路
- iOS視頻錄制(或選擇)壓縮及上傳功能(整理)
- iOS仿微信相機(jī)拍照、視頻錄制功能
- 詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法
- iOS中視頻播放器的簡(jiǎn)單封裝詳解
- iOS中讀取照片庫(kù)及保存圖片或視頻到照片庫(kù)的要點(diǎn)解析
- iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼
- iOS視頻中斷后臺(tái)音樂播放的處理方法
相關(guān)文章
iOS仿微信添加標(biāo)簽效果(shape實(shí)現(xiàn))
微信做的用戶體驗(yàn)非常棒,今天用shape來(lái)做下微信的標(biāo)簽功能,非常不錯(cuò),對(duì)ios 仿微信添加標(biāo)簽功能感興趣的朋友一起看看吧2016-11-11objc方法聲明和實(shí)現(xiàn)由于參數(shù)類型不一致所引發(fā)的崩潰
這篇文章主要為大家介紹了objc方法聲明和實(shí)現(xiàn)由于參數(shù)類型不一致所引發(fā)的崩潰詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03iOS通過(guò)攝像頭圖像識(shí)別技術(shù)分享
本篇文章給大家詳細(xì)講述了讓IOS開發(fā)中通過(guò)攝像頭進(jìn)行圖像識(shí)別的相關(guān)技術(shù),對(duì)此有興趣的朋友參考學(xué)習(xí)下吧。2018-02-02詳解Obejective-C中將JSON數(shù)據(jù)轉(zhuǎn)為模型的方法
這篇文章主要介紹了Obejective-C中JSON數(shù)據(jù)轉(zhuǎn)為模型的方法,同時(shí)介紹了使用jastor庫(kù)的方法,需要的朋友可以參考下2016-03-03iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單
這篇文章主要介紹了 iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單的相關(guān)資料,需要的朋友可以參考下2016-12-12實(shí)例講解iOS音樂播放器DOUAudioStreamer用法
本篇文章給大家通過(guò)實(shí)例講解了iOS音樂播放器DOUAudioStreamer用法以及分享了實(shí)例代碼,一起學(xué)習(xí)參考下吧。2017-12-12iOS緩存文件大小顯示功能和一鍵清理功能的實(shí)現(xiàn)方法
緩存占用了系統(tǒng)的大量空間,如何實(shí)時(shí)動(dòng)態(tài)的顯示緩存的大小,使用戶清晰的了解緩存的積累情況,有效的進(jìn)行一鍵清理呢?下面小編通過(guò)本文給大家介紹iOS緩存文件大小顯示功能和一鍵清理功能的實(shí)現(xiàn)方法,一起看看吧2016-10-10iOS實(shí)現(xiàn)鎖屏頁(yè)面控制音樂播放
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)鎖屏頁(yè)面控制音樂播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12