#import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通过实例化的控制器来设置player属性
#import <AVKit/AVKit.h> // 1. 导入头文件 iOS 9 新增
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [[UIButton alloc]initWithFrame:self.view.frame];
btn.backgroundColor = [UIColor redColor];
[btnsetTitle:@"点击播放" forState:UIControlStateNormal];
[btnaddTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)btnClick{
// 本地资源文件
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"1.mp4" ofType:nil];
// 2. 创建视频播放控制器
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
// 3. 设置视频播放器 (这里为了简便,使用了URL方式,同样支持playerWithPlayerItem:的方式)
playerViewController.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];
// 4. modal展示
[self presentViewController:playerViewController animated:YES completion:nil];
// [self presentViewController:playerViewController animated:YES completion:^{
// [playerViewController.player play];
// }];
// 5. 开始播放 : 默认不会自动播放
[playerViewController.playerplay];
}
网友评论