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
在使用前在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
相关文章推荐
- 【转】iOS Provisioning Profile(Certificate)与Code Signing详解 -- 待看
- iOS 疑难杂症 — — 推送本地国际化 loc-key 本地化失败的问题
- iOS通过dSYM文件分析crash
- iOS开发cell自定义高亮和选中背景颜色
- iOS开发服务器返回(null) 与<null>的处理
- iOS addChildViewController 详解
- Nagios安装完后status map,trends等页面访问出错之解决
- IOS上架审核问题
- iOS学习之—— initWithFrame的理解
- iOS学习之——NSCoding协议
- iOS五种本地缓存数据方式
- iOS开发git使用过程中需要忽略的文件解决办法
- iOS-WKWebView特性以及与JS交互的一些事
- 【转】iOS开发者申请发布证书及真机调试图文详解
- 【转】iOS开发者账号和证书
- nagios监控3306端口
- iphone开发简介(16.5.19)
- iOS 异步加载刷新
- IOS 关闭键盘 退出键盘 的5种方式
- 你所访问的站点在微博的认证失败 21322