您的位置:首页 > 移动开发 > IOS开发

iOS9 视频播放器 AVPlayerViewController的简单使用

2016-05-19 15:05 141 查看
iOS9之后视频播放器废弃了MPMidMediuPlayerController,现在使用AVPlayerViewController也可实现视频播放的功能,而且在iPad上带有画中画的效果。

在使用前在plist文件中把网络的一个属性改一下,不然http的请求拿不到数据,具体操作可以看上一篇/article/9964244.html

效果如图





ViewController.m文件

#import "ViewController.h"

#import <AVKit/AVKit.h>

#import <AVFoundation/AVFoundation.h>

@interface
ViewController ()

{

AVPlayerViewController * avPlayer;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIButton * playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];

playVideoButton.center =
self.view.center;

playVideoButton.backgroundColor = [UIColor
grayColor];

[playVideoButton
setTitle:@"点击播放"
forState:UIControlStateNormal];

[playVideoButton
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

[playVideoButton
addTarget:self
action:@selector(playeVideo)
forControlEvents:UIControlEventTouchUpInside];

[self.view
addSubview:playVideoButton];

}

- (void)playeVideo

{

//http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4

NSURL * videoURL = [NSURL
URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];

avPlayer = [[AVPlayerViewController
alloc]
init];

avPlayer.player = [[AVPlayer
alloc]
initWithURL:videoURL];

/*

可以设置的值及意义如下:

AVLayerVideoGravityResizeAspect
不进行比例缩放 以宽高中长的一边充满为基准

AVLayerVideoGravityResizeAspectFill
不进行比例缩放 以宽高中短的一边充满为基准

AVLayerVideoGravityResize
进行缩放充满屏幕

*/

avPlayer.videoGravity =
AVLayerVideoGravityResizeAspect;

[self
presentViewController:avPlayer
animated:YES
completion:nil];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: