美文网首页
ios 播放本地视频

ios 播放本地视频

作者: 你又脸红了i | 来源:发表于2018-11-15 10:24 被阅读0次

#import "ViewController.h"

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

}

相关文章

网友评论

      本文标题:ios 播放本地视频

      本文链接:https://www.haomeiwen.com/subject/ktiefqtx.html