一. 視頻播放介紹
實現(xiàn)方案四種:
1.AVPlayer
> 優(yōu)點:
可以自定義UI, 進行控制
> 缺點:
單純的播放, 沒有控制UI, 而且如果要顯示播放界面, 需要借助AVPlayerLayer, 添加圖層到需要展示的圖層上
2.MPMoviePlayerControlle
> 優(yōu)點:
自帶的播放控制UI, 不需要手動添加
> 缺點:
不能自定義UI
只能將此控制器視圖添加到其他視圖進行展示
此控制器不是視圖控制器, 不能彈出
3.MPMoviePlayerViewController
> 優(yōu)點:
自帶的播放控制UI, 不需要手動添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動調(diào)整視圖大小, 添加到其他視圖上
> 缺點:
不能自定義UI
針對于第2種和第3種實現(xiàn)方案, 在iOS9.0之后, 統(tǒng)一使用
4.AVPlayerViewController
> 優(yōu)點:
自帶的播放控制UI, 不需要手動添加
此控制器是視圖控制器, 可以彈出, 可以壓棧
也可以手動調(diào)整視圖大小, 添加到其他視圖上
> 缺點:
不能自定義UI
二. 使用AVPlayer 播放遠程視頻
1.實現(xiàn)播放功能
- 1.1通過遠程URL創(chuàng)建AVPlayer對象
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
_player = [AVPlayer playerWithURL:remoteURL];```
- 1.2開始播放
[self.player play];```
- 1.3問題
只能播放聲音, 看不到圖像
解決方案: 需要借助AVPlayerLayer對象, 根據(jù)player創(chuàng)建圖層, 添加到視圖上
2. 實現(xiàn)視頻顯示功能
- 2.1根據(jù)player對象, 創(chuàng)建 AVPlayerLayer 對象
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];```
- 2.2設(shè)置圖層 AVPlayerLayer 的大小
layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 16);```
- 2.3添加到需要展示的視圖上即可
[self.view.layer addSublayer:layer];```
###3. 封裝專門用于播放的視圖
- 主要封裝一些操作的工具條
- 備注
iOS9.0 網(wǎng)絡(luò)請求適配 (HTTPS-->HTTP)
NSAppTransportSecurity NSAllowsArbitraryLoads
***
***
#三. 使用MPMoviePlayerController播放視頻
####相比于AVPlayer播放, 自帶一些控制按鈕
- 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];```
注意: 此控制器不是視圖控制器, 不能直接彈出
播放器的播放狀態(tài), 是通過通知的方式告訴外界
####iOS9.0之后, 需要使用AVPlayerViewController
- 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, 添加到需要展示的視圖上
// 設(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];```
- 5.播放
// 開始播放
[self.playerVC.player play];```
***
***
#四. 使用MPMoviePlayerViewController播放視頻
###實現(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];```
###iOS9.0之后, 需要使用AVPlayerViewController
- 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, 添加到需要展示的視圖上
// 設(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];```
- 5.播放
// 開始播放
[self.playerVC.player play];```
#WMPlayer(播放器)
![VMPlayer.gif](http://upload-images.jianshu.io/upload_images/1429890-2546b25ef6a6c5f1.gif?imageMogr2/auto-orient/strip)
***
***
***
- #github
| 項目 | 簡介 |
| : | : |
| [MGDS_Swif](https://github.com/LYM-mg/MGDS_Swift) | 逗視視頻直播 |
| [MGMiaoBo](https://github.com/LYM-mg/MGMiaoBo) | 喵播視頻直播 |
| [MGDYZB](https://github.com/LYM-mg/MGDYZB) | 斗魚視頻直播 |
| [MGDemo](https://github.com/LYM-mg/MGDemo) | n多小功能合集 |
| [MGBaisi](https://github.com/LYM-mg/MGBaisi) | 高度仿寫百思 |
| [MGSinaWeibo](https://github.com/LYM-mg/MGSinaWeibo) | 高度仿寫Sina |
| [MGLoveFreshBeen](https://github.com/LYM-mg/MGLoveFreshBeen) | 一款電商App |
| [MGWeChat](https://github.com/LYM-mg/MGWeChat) | 小部分實現(xiàn)微信功能 |
| [MGTrasitionPractice](https://github.com/LYM-mg/MGTrasitionPractice) | 自定義轉(zhuǎn)場練習(xí) |
| [DBFMDemo](https://github.com/LYM-mg/DBFMDemo) | 豆瓣電臺 |
| [MGPlayer](https://github.com/LYM-mg/MGPlayer) | 一個播放視頻的Demo |
| [MGCollectionView](https://github.com/LYM-mg/MGCollectionView) | 環(huán)形圖片排布以及花瓣形排布 |
| [MGPuBuLiuDemo](https://github.com/LYM-mg/MGPuBuLiuDemo) | 瀑布流--商品展 |
| [MGSlideViewDemo](https://github.com/LYM-mg/MGSlideViewDemo) | 一個簡單點的側(cè)滑效果慕爬,仿QQ側(cè)滑 |
| [MyResume](https://github.com/LYM-mg/MyResume) | 一個展示自己個人簡歷的Demo |
| [GoodBookDemo](https://github.com/LYM-mg/GoodBookDemo) | 好書 |
- #[1铲敛、直播喵播MGMiaoBo下載](https://github.com/LYM-mg/MGMiaoBo)
![Snip20161026_15.png](http://upload-images.jianshu.io/upload_images/1429890-5c0296ffb33d18e2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![Snip20161026_16.png](http://upload-images.jianshu.io/upload_images/1429890-9ca835b72a5b053a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![Snip20161026_35.png](http://upload-images.jianshu.io/upload_images/1429890-0e19cf9d25ed0c27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- #[2扒披、逗視:逗你玩的直播App杖挣,可下載試玩](https://github.com/LYM-mg/MGDS_Swift)
- >#看下效果
![逗視介紹1.gif](http://upload-images.jianshu.io/upload_images/1429890-ecd25e08d367c32e.gif?imageMogr2/auto-orient/strip)
![逗視介紹2.gif](http://upload-images.jianshu.io/upload_images/1429890-91b427263bc09abd.gif?imageMogr2/auto-orient/strip)
***