ios 点击按钮跳转视频播放

#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];


}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容