iOS之AVPlayerViewController的使用oc
生活随笔
收集整理的這篇文章主要介紹了
iOS之AVPlayerViewController的使用oc
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iOS之AVPlayerViewController的使用oc
// // ViewController.m // AVPlayer01-oc // // Created by 魯軍 on 2021/4/11. //#import "ViewController.h" #import <AVKit/AVKit.h>#define W self.view.frame.size.width #define H self.view.frame.size.height@interface ViewController () {AVPlayerViewController *_vc;NSString *_strUrl;//********AVPlayer *_player;AVPlayerLayer *imageLayer;UIProgressView *progress; } @property(nonatomic ,strong)AVPlayerViewController*avPlayViewController; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{[self test1];// [self playMV];} -(void)test2{//NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:nil];// NSURL *url = [NSURL fileURLWithPath:path];NSString *str= @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";NSURL *_strUrl1 = [_strUrl hasPrefix:@"http"] ? [NSURL URLWithString:str] : [NSURL fileURLWithPath:str];_player = [[AVPlayer alloc] initWithURL:_strUrl1];imageLayer = [AVPlayerLayer playerLayerWithPlayer:_player];imageLayer.frame = CGRectMake(0, 0, W, H);[self.view.layer addSublayer:imageLayer];//設置播放速度_player.rate=1.5;//播放[_player play];//暫停[_player pause];NSLog(@"12312");}//4.添加視頻播放的進度條 -(void)addProgressView{progress=[[UIProgressView alloc]initWithFrame:CGRectMake(10,H-20, W-20, 10)];[self.view addSubview:progress];__weak UIProgressView *p = progress;[_player addPeriodicTimeObserverForInterval:CMTimeMake(1,10 ) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {//更新進度//總時間float total = _player.currentItem.duration.value / _player.currentItem.duration.timescale;//當前時間float current = time.value * 1.0f/time.timescale;p.progress = current/total;}]; }//2.2 AVPlayerViewController播放實現 -(void)playMV{//創建視頻的播放路徑//播放本地文件// NSString *PlayPath=[[NSBundle mainBundle]pathForResource:@"我是藝術.mp4" ofType:nil];// NSURL *url=[NSURL fileURLWithPath:PlayPath];//播放網絡文件NSString *NetPath=@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";NSURL *urlNet=[NSURL URLWithString:NetPath];//創建一個播放器AVPlayer *player=[AVPlayer playerWithURL:urlNet];//實例化播放視頻控制器_avPlayViewController=[[AVPlayerViewController alloc]init];_avPlayViewController.player=player;//模態跳轉[self presentViewController:_avPlayViewController animated:YES completion:nil];//[player play]; }-(void)test1{_vc = [[AVPlayerViewController alloc] init];_vc.view.frame = self.view.bounds;NSURL *url = [[NSBundle mainBundle] URLForResource:@"本地視頻.mp4" withExtension:nil];_strUrl = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";NSURL *_strUrl1 = [_strUrl hasPrefix:@"http"] ? [NSURL URLWithString:_strUrl] : [NSURL fileURLWithPath:_strUrl];_vc.player = [AVPlayer playerWithURL:_strUrl1];_vc.showsPlaybackControls = YES;_vc.entersFullScreenWhenPlaybackBegins = YES;//開啟這個播放的時候支持(全屏)橫豎屏哦_vc.exitsFullScreenWhenPlaybackEnds = YES;//開啟這個所有 item 播放完畢可以退出全屏[self.view addSubview:_vc.view];if(_vc.readyForDisplay){[_vc.player play];}}@end總結
以上是生活随笔為你收集整理的iOS之AVPlayerViewController的使用oc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS之Swift的CoreData入门
- 下一篇: 场外科目三考试技巧