一. 視頻播放介紹
實(shí)現(xiàn)方案四種
- AVPlayer
優(yōu)點(diǎn):
可以自定義UI, 進(jìn)行控制
缺點(diǎn):
單純的播放, 沒有控制UI, 而且如果要顯示播放界面, 需要借助AVPlayerLayer, 添加圖層到需要展示的圖層上
- MPMoviePlayerController
優(yōu)點(diǎn):
自帶的播放控制UI, 不需要手動(dòng)添加
缺點(diǎn):
不能自定義UI
只能將此控制器視圖添加到其他視圖進(jìn)行展示
此控制器不是視圖控制器, 不能彈出
- MPMoviePlayerViewController
優(yōu)點(diǎn):
自帶的播放控制UI, 不需要手動(dòng)添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動(dòng)調(diào)整視圖大小, 添加到其他視圖上
缺點(diǎn):
不能自定義UI
- 針對(duì)于第2種和第3種實(shí)現(xiàn)方案, 在iOS9.0之后, 統(tǒng)一使用AVPlayerViewController
優(yōu)點(diǎn):
自帶的播放控制UI, 不需要手動(dòng)添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動(dòng)調(diào)整視圖大小, 添加到其他視圖上
缺點(diǎn):
不能自定義UI
二. 使用AVPlayer 播放遠(yuǎn)程視頻
1. 實(shí)現(xiàn)播放功能
通過遠(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];-
開始播放
[self.player play];
存在問題
只能播放聲音, 看不到圖像
解決方案: 需要借助AVPlayerLayer對(duì)象, 根據(jù)player創(chuàng)建圖層, 添加到視圖上
2. 實(shí)現(xiàn)視頻顯示功能
-
根據(jù)player對(duì)象, 創(chuàng)建 AVPlayerLayer 對(duì)象
AVPlayerLayer*layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
-
設(shè)置圖層 AVPlayerLayer 的大小
layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 16);
-
添加到需要展示的視圖上即可
[self.view.layer addSublayer:layer];
3. 封裝專門用于播放的視圖
主要封裝一些操作的工具條
備注
iOS9.0 網(wǎng)絡(luò)請(qǐng)求適配 (HTTPS-->HTTP)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
三. 使用MPMoviePlayerController播放視頻
相比于AVPlayer播放, 自帶一些控制按鈕
-
導(dǎo)入框架
#import <MediaPlayer/MediaPlayer.h>
-
根據(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];
-
設(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];
-
播放
[self.moviePlayer play];
注意: 此控制器不是視圖控制器, 不能直接彈出
播放器的播放狀態(tài), 是通過通知的方式告訴外界
iOS9.0之后, 需要使用AVPlayerViewController
-
導(dǎo)入框架
#import <AVFoundation/AVFoundation.h> #import <AVKit/AVKit.h>
-
根據(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];
-
根據(jù)AVPlayer, 創(chuàng)建AVPlayerViewController控制器
_playerVC = [[AVPlayerViewController alloc] init]; _playerVC.player = player;
設(shè)置播放視圖frame, 添加到需要展示的視圖上
// 設(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];
或者
[self presentViewController:self.playerVC animated:YES completion:nil];
- 播放
// 開始播放
[self.playerVC.player play];
四. 使用MPMoviePlayerViewController播放視頻
1. 實(shí)現(xiàn)步驟
-
導(dǎo)入框架
#import <MediaPlayer/MediaPlayer.h>
-
根據(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];
-
直接模態(tài)彈出該控制器(或者: 設(shè)置播放視圖frame, 添加到需要展示的視圖上)
[self presentViewController:self.playerVC animated:YES completion:^{ [self.playerVC.moviePlayer play]; }];
-
播放
[self.playerVC.moviePlayer play];
iOS9.0之后, 需要使用AVPlayerViewController
-
導(dǎo)入框架
#import <AVFoundation/AVFoundation.h> #import <AVKit/AVKit.h>
-
根據(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];
-
根據(jù)AVPlayer, 創(chuàng)建AVPlayerViewController控制器
_playerVC = [[AVPlayerViewController alloc] init]; _playerVC.player = player;
設(shè)置播放視圖frame, 添加到需要展示的視圖上
// 設(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];
或者
[self presentViewController:self.playerVC animated:YES completion:nil];
- 播放
// 開始播放
[self.playerVC.player play];