ios 點擊按鈕跳轉(zhuǎn)視頻播放

#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. 開始播放 : 默認(rèn)不會自動播放

? ? [playerViewController.playerplay];


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容