1 AVPlayer
(1) 優(yōu)缺點
優(yōu)點:可以自定義 UI, 進(jìn)行控制
缺點:單純的播放,沒有控制 UI(進(jìn)度,暫停,播放等按鈕),而且如果要顯示播放界面, 需要借助AVPlayerLayer, 添加圖層到需要展示的圖層上
(2)實現(xiàn)遠(yuǎn)程視頻播放
1.導(dǎo)入框架#import <AVFoundation/AVFoundation.h>
2.通過遠(yuǎn)程 URL 創(chuàng)建 AVPlayer 對象
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
_player = [AVPlayer playerWithURL:remoteURL];
3.開始播放
[self.player play];
存在問題:只能播放聲音, 看不到圖像
解決方案: 需要借助AVPlayerLayer對象, 根據(jù)player創(chuàng)建圖層, 添加到視圖上
4.根據(jù) player 對象創(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)缺點
優(yōu)點:自帶的播放控制UI(進(jìn)度條,上一個,下一個,暫停), 不需要手動添加
缺點:不能自定義UI
只能將此控制器視圖添加到其他視圖進(jìn)行展示
此控制器不是視圖控制器, 不能直接彈出
播放器的播放狀態(tài), 是通過通知的方式告訴外界
(2)視頻播放實現(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)缺點
優(yōu)點:自帶的播放控制UI, 不需要手動添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動調(diào)整視圖大小, 添加到其他視圖上
缺點:不能自定義UI
(2)視頻播放實現(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 針對于第2種和第3種實現(xiàn)方案, 在iOS9.0之后, 統(tǒng)一使用AVPlayerViewController
(1)優(yōu)缺點
優(yōu)點:自帶的播放控制UI, 不需要手動添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動調(diào)整視圖大小, 添加到其他視圖上
缺點:不能自定義UI
(2)視頻播放實現(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];
??appstore審核問答群:369250107,建了個倉庫:,github專門解決蘋果拒絕各種問題匯總的倉庫