媒体播放器 AVPlayer 与 AVPlayerViewController

简介

  • AVPlayer是一个可以播放任何格式的全功能影音播放器,使用AVPlayer需导入AVFoundation.h。

  • 支持视频格式: WMV,AVI,MKV,RMVB,RM,XVID,MP4,3GP,MPG等。

  • 支持音频格式:MP3,WMA,RM,ACC,OGG,APE,FLAC,FLV等。

  • 在开发中,单纯使用AVPlayer类是无法显示视频的,要将视频层添加至AVPlayerLayer中,这样才能将视频显示出来。

  • AVPlayer并未提供视频操作组件,需用户自定义。

初始化方法

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">+ (instancetype)playerWithURL:(NSURL <span class="hljs-strong" style="box-sizing: border-box;">*)URL;

</span>+ (instancetype)playerWithPlayerItem:(AVPlayerItem <span class="hljs-strong" style="box-sizing: border-box;">*)item;

</span><span class="hljs-bullet" style="box-sizing: border-box;">- </span>(instancetype)initWithURL:(NSURL <span class="hljs-strong" style="box-sizing: border-box;">*)URL;

</span><span class="hljs-bullet" style="box-sizing: border-box;">- </span>(instancetype)initWithPlayerItem:(AVPlayerItem <span class="hljs-strong" style="box-sizing: border-box;">*)item;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

常用属性

  • status:获取播放媒体状态

  • rate:设置播放速度

常用方法

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1、播放</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)play;

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2、暂停</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)pause;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

AVPlayerLayer

AVPlayerLayer主要用于显示视频;

1、初始化方法

<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1、遍历初始化</span>
+ (instancetype)layer;

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2、alloc + init</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3、根据播放媒体初始化</span>
+ (AVPlayerLayer *)playerLayerWithPlayer:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nullable</span> AVPlayer *)player;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

2、常用属性

  • bounds:设置播放矩形区域;

  • position:设置播放区域中心点位置;

  • videoGravity:设置拉伸模式

  • player:设置播放媒体

AVPlayerViewController

  • AVPlayerViewController提供了默认的可视化控制界面,要使用AVPlayerViewController需导入AVKit.h。

  • AVPlayerViewController整合了一个完整的播放器,可以作为控制器进行操作显示。

  • AVPlayerViewController可以支持播放本地及网络视频文件,支持以下格式:.mov、.mp4、.mpv、.3gp。

常用属性

  • player:设置播放器

  • showsPlaybackControls:设置是否显示媒体播放组件,默认YES

  • videoGravity:设置视频拉伸模式

  • allowsPictureInPicturePlayback:设置是否允许画中画回放,默认YES

  • delegate:设置代理

AVPlayerViewControllerDelegate

<code class="hljs erlang has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">// <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>、即将开始画中画
<span class="hljs-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>playerViewControllerWillStartPictureInPicture:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">AVPlayerViewController</span> *)</span>playerViewController;

// 2、开始画中画
- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>playerViewControllerDidStartPictureInPicture:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">AVPlayerViewController</span> *)</span>playerViewController;

// 3、画中画失败
- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>playerViewController:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">AVPlayerViewController</span> *)</span>playerViewController failedToStartPictureInPictureWithError:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSError</span> *)</span>error;

// 4、即将结束画中画
- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>playerViewControllerWillStopPictureInPicture:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">AVPlayerViewController</span> *)</span>playerViewController;

// 5、结束画中画
- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>playerViewControllerDidStopPictureInPicture:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">AVPlayerViewController</span> *)</span>playerViewController;
</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

媒体播放器案例

本案例主要讲解播放视频的使用方法;

效果展示

代码示例

<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">#<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ViewController.h"</span>
#<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> <AVKit/AVKit.h>
#<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> <AVFoundation/AVFoundation.h>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">enum</span> {
    DirectPlayBtnTag = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>,
    FullScreenPlayBtnTag
};

<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@interface</span> ViewController () <AVPlayerViewControllerDelegate> {
    AVPlayer *_player; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 媒体播放器 */</span>
    AVPlayerViewController *_playerViewController; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 媒体播放控制器 */</span>

}

<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@property</span> (nonatomic, strong) UIButton *directPlayBtn;     <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 直接播放按钮 */</span>
<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@property</span> (nonatomic, strong) UIButton *fullscreenPlayBtn; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 全屏播放 */</span>

<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@property</span> (nonatomic, strong) AVPlayer *player; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 媒体播放器 */</span>
<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@property</span> (nonatomic, strong) AVPlayerViewController *playerViewController; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 媒体播放控制器 */</span>

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)initializeUserInterface; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**< 初始化用户界面 */</span>

<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@end</span>

<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@implementation</span> ViewController

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad {
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];
    [self initializeUserInterface];
}

#pragma mark *** Initialize methods ***

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)initializeUserInterface {
    self.title = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"音乐播放器"</span>;
    self.view.backgroundColor = [UIColor whiteColor];

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 加载视图</span>
    [self.view addSubview:self.directPlayBtn];
    [self.view addSubview:self.fullscreenPlayBtn];
}

#pragma mark *** Events ***
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)respondsToButton:(UIButton *)sender {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">switch</span> (sender.tag) {
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 直接播放</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> DirectPlayBtnTag: {

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (_player) {
                [_player pause];
                _player = nil;
            }

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (_playerViewController) {
                [_playerViewController removeFromParentViewController];
                [_playerViewController.view removeFromSuperview];
                _playerViewController = nil;
            }

            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1、获取媒体资源地址</span>
            NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"宣传资料.mp4"</span>];
            NSURL *sourceMovieURL = [NSURL fileURLWithPath:path];

            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2、创建AVPlayerItem</span>
            AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:sourceMovieURL];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3、根据AVPlayerItem创建媒体播放器</span>
            _player = [AVPlayer playerWithPlayerItem:playerItem];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4、创建AVPlayerLayer,用于呈现视频</span>
            AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 5、设置显示大小和位置</span>
            playerLayer.bounds = CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">350</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>);
            playerLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64</span> + CGRectGetMidY(playerLayer.bounds) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 6、设置拉伸模式</span>
            playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 7、获取播放持续时间</span>
            NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%lld"</span>, playerItem.duration.value);
            [_player play];
            [self.view.layer addSublayer:playerLayer];
        }
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 全屏播放</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> FullScreenPlayBtnTag: {

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (_player) {
                [_player pause];
                _player = nil;
            }

            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化URL资源地址</span>

            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取网络资源地址</span>
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// + (nullable instancetype)URLWithString:(NSString *)URLString;</span>

            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1、获取本地资源地址</span>
            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"宣传资料.mp4"</span>]];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2、初始化媒体播放控制器</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (_playerViewController) {
                _playerViewController = nil;
            }
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3、配置媒体播放控制器</span>
            _playerViewController = [[AVPlayerViewController alloc]  init];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置媒体源数据</span>
            _playerViewController.player = [AVPlayer playerWithURL:url];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置拉伸模式</span>
            _playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置是否显示媒体播放组件</span>
            _playerViewController.showsPlaybackControls = YES;
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置大力</span>
            _playerViewController.delegate = self;
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 播放视频</span>
            [_playerViewController.player play];
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置媒体播放器视图大小</span>
            _playerViewController.view.bounds = CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">350</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>);
            _playerViewController.view.center = CGPointMake(CGRectGetMidX(self.view.bounds), <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64</span> + CGRectGetMidY(_playerViewController.view.bounds) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4、推送播放</span>
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 推送至媒体播放器进行播放</span>
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// [self presentViewController:_playerViewController animated:YES completion:nil];</span>
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 直接在本视图控制器播放</span>
            [self addChildViewController:_playerViewController];
            [self.view addSubview:_playerViewController.view];

        }
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;

        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">default</span>:
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;
    }
}

#pragma mark *** AVPlayerViewControllerDelegate ***
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, NSStringFromSelector(_cmd));
}

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, NSStringFromSelector(_cmd));
}

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, NSStringFromSelector(_cmd));
}

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, NSStringFromSelector(_cmd));
}

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, NSStringFromSelector(_cmd));
}
#pragma mark *** Getters ***
- (UIButton *)directPlayBtn {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!_directPlayBtn) {
        _directPlayBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _directPlayBtn.bounds = CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>);
        _directPlayBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds) - CGRectGetMidX(_directPlayBtn.bounds), CGRectGetMidY(self.view.bounds) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
        _directPlayBtn.tag = DirectPlayBtnTag;
        [_directPlayBtn setTitle:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"直接播放"</span> forState:UIControlStateNormal];
        [_directPlayBtn addTarget:self action:<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@selector</span>(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _directPlayBtn;
}

- (UIButton *)fullscreenPlayBtn {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!_fullscreenPlayBtn) {
        _fullscreenPlayBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _fullscreenPlayBtn.bounds = CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);
        _fullscreenPlayBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds) + CGRectGetMidX(_fullscreenPlayBtn.bounds), CGRectGetMidY(self.view.bounds) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
        _fullscreenPlayBtn.tag = FullScreenPlayBtnTag;
        [_fullscreenPlayBtn setTitle:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"全屏播放"</span> forState:UIControlStateNormal];
        [_fullscreenPlayBtn addTarget:self action:<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@selector</span>(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];

    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _fullscreenPlayBtn;
}
<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@end</span></code>

你可能感兴趣的:(ios,播放器,avplayer)