#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通過實例化的控制器來設(shè)置player屬性
#import <AVKit/AVKit.h>? // 1. 導(dǎo)入頭文件? 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. 創(chuàng)建視頻播放控制器
? ? AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
? ? // 3. 設(shè)置視頻播放器 (這里為了簡便,使用了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];
}