视频播放

一. 视频播放介绍

实现方案四种:

1.AVPlayer

  > 优点: 
   可以自定义UI, 进行控制
  > 缺点:
   单纯的播放, 没有控制UI, 而且如果要显示播放界面, 需要借助AVPlayerLayer, 添加图层到需要展示的图层上

2.MPMoviePlayerControlle

 > 优点: 
   自带的播放控制UI, 不需要手动添加
 > 缺点:
   不能自定义UI
   只能将此控制器视图添加到其他视图进行展示
   此控制器不是视图控制器, 不能弹出

3.MPMoviePlayerViewController

 > 优点: 
   自带的播放控制UI, 不需要手动添加
   此控制器是视图控制器, 可以弹出, 可以压栈
   也可以手动调整视图大小, 添加到其他视图上
> 缺点:
   不能自定义UI

针对于第2种和第3种实现方案, 在iOS9.0之后, 统一使用

4.AVPlayerViewController

> 优点: 
  自带的播放控制UI, 不需要手动添加
  此控制器是视图控制器, 可以弹出, 可以压栈
  也可以手动调整视图大小, 添加到其他视图上
> 缺点:
  不能自定义UI


二. 使用AVPlayer 播放远程视频

1.实现播放功能

  • 1.1通过远程URL创建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对象, 根据player创建图层, 添加到视图上

2. 实现视频显示功能

  • 2.1根据player对象, 创建 AVPlayerLayer 对象
 AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];```

- 2.2设置图层 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 网络请求适配 (HTTPS-->HTTP)

        NSAppTransportSecurity NSAllowsArbitraryLoads
***
***

#三. 使用MPMoviePlayerController播放视频
####相比于AVPlayer播放, 自带一些控制按钮
- 1.导入框架

import ```

  • 2.根据URL, 创建控制器 MPMoviePlayerController
  NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:remoteURL];```

- 3.设置播放视图frame, 添加到需要展示的视图上

// 设置播放视图的frame
self.moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 9 / 16);
// 设置播放视图控制样式
self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
// 添加播放视图到要显示的视图
[self.view addSubview:self.moviePlayer.view];```

  • 4.播放
 [self.moviePlayer play];```

注意: 此控制器不是视图控制器, 不能直接弹出
播放器的播放状态, 是通过通知的方式告诉外界

####iOS9.0之后, 需要使用AVPlayerViewController
- 1.导入框架

import

import ```

  • 2.根据URL创建AVPlayer
 NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 AVPlayer *player = [AVPlayer playerWithURL:remoteURL];```

- 3.根据AVPlayer, 创建AVPlayerViewController控制器

_playerVC = [[AVPlayerViewController alloc] init];
_playerVC.player = player;```

  • 4.设置播放视图frame, 添加到需要展示的视图上
 // 设置播放视图的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播放视频
###实现步骤
- 1.导入框架

import ```

  • 2.根据URL, 创建控制器 MPMoviePlayerViewController
  NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 _playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:remoteURL];```

- 3.直接模态弹出该控制器(或者: 设置播放视图frame, 添加到需要展示的视图上)

[self presentViewController:self.playerVC animated:YES completion:^{
[self.playerVC.moviePlayer play];
}];```

  • 4.播放
 [self.playerVC.moviePlayer play];```


###iOS9.0之后, 需要使用AVPlayerViewController
- 1.导入框架

import

import ```

  • 2.根据URL创建AVPlayer
 NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
 AVPlayer *player = [AVPlayer playerWithURL:remoteURL];```

- 3.根据AVPlayer, 创建AVPlayerViewController控制器

_playerVC = [[AVPlayerViewController alloc] init];
_playerVC.player = player;```

  • 4.设置播放视图frame, 添加到需要展示的视图上
 // 设置播放视图的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)   |  小部分实现微信功能   | 
    |  [MGTrasitionPractice](https://github.com/LYM-mg/MGTrasitionPractice)   |  自定义转场练习   | 
    |  [DBFMDemo](https://github.com/LYM-mg/DBFMDemo)  |  豆瓣电台   | 
    | [MGPlayer](https://github.com/LYM-mg/MGPlayer)  |  一个播放视频的Demo   | 
    |  [MGCollectionView](https://github.com/LYM-mg/MGCollectionView)  |  环形图片排布以及花瓣形排布   | 
    |  [MGPuBuLiuDemo](https://github.com/LYM-mg/MGPuBuLiuDemo)  |  瀑布流--商品展   | 
    |  [MGSlideViewDemo](https://github.com/LYM-mg/MGSlideViewDemo)  |  一个简单点的侧滑效果,仿QQ侧滑   | 
    | [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)
***

你可能感兴趣的:(视频播放)