视频播放

作者: Mg明明就是你 | 来源:发表于2016-04-28 09:40 被阅读961次

一. 视频播放介绍

实现方案四种:

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 <MediaPlayer/MediaPlayer.h>```

  • 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 <AVFoundation/AVFoundation.h>

import <AVKit/AVKit.h>```

  • 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 <MediaPlayer/MediaPlayer.h>```

  • 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 <AVFoundation/AVFoundation.h>

import <AVKit/AVKit.h>```

  • 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:https://img.haomeiwen.com/i1429890/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:https://img.haomeiwen.com/i1429890/5c0296ffb33d18e2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![Snip20161026_16.png](http:https://img.haomeiwen.com/i1429890/9ca835b72a5b053a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![Snip20161026_35.png](http:https://img.haomeiwen.com/i1429890/0e19cf9d25ed0c27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
   - #[2、逗视:逗你玩的直播App,可下载试玩](https://github.com/LYM-mg/MGDS_Swift)
  - >#看下效果
![逗视介绍1.gif](http:https://img.haomeiwen.com/i1429890/ecd25e08d367c32e.gif?imageMogr2/auto-orient/strip)
![逗视介绍2.gif](http:https://img.haomeiwen.com/i1429890/91b427263bc09abd.gif?imageMogr2/auto-orient/strip)
***

相关文章

  • 初级视频播放功能

    打开相册选择视频 使用系统播放器播放视频 使用VideoView播放视频 使用SurfaceView播放视频 vo...

  • 3.4 音频播放.视频播放.相册调用.相机调用

    音频播放.视频播放.相册调用.相机调用 音频播放 视频播放 相册调用 视频音频资源 视频音频资源.png

  • 视频播放器

    系统播放器 打开视频列表 调用系统播放器播放视频 调用系统播放器播放网络视频 VideoView播放器 调用 V...

  • 视频播放

  • 视频播放

    import "ViewController.h" import "ZSPlayerView.h" @interf...

  • 视频播放

    NSString *file = [[NSBundle mainBundle]pathForResource:@"...

  • 视频播放

    视频播放器的实现有多种方式,此处主要针对主流方式实现一个播放器 MediaPlayer框架

  • 视频播放

    一. 视频播放介绍 实现方案四种: 1.AVPlayer 2.MPMoviePlayerControlle 3.M...

  • 播放视频

    主要使用 VideoView 类来实现。和 MediaPlayer 比较类似。 VideoView工作流程 1. ...

  • 视频播放

    1.依赖compile 'fm.jiecao:jiecaovideoplayer:2.0' compile'fm....

网友评论

  • Aprilx:mark!MP这个很爽呀
  • Caiflower:最后一个那个DEMO有么
    Mg明明就是你:@花菜ChrisCai 有滴https://github.com/LYM-mg/MGPlarer
  • 张云龙:请问如何解决使用AVPlayer播放时,手机静音,播放的音频或者视频没有声音的问题?
    Mg明明就是你:@张云龙 唔知喔,没有研究过,有空研究一下
  • 赵小噢:看见这头像忍不住点进来了,虽然知道不会是她
    IAMCJ:@Mg明明就是你 在努力学习的我分分钟出戏了 :cold_sweat:
    Mg明明就是你: @蛋疼疼不疼 😓哈哈,重情

本文标题:视频播放

本文链接:https://www.haomeiwen.com/subject/ezthrttx.html