AVPlayerViewController

demo地址:https://github.com/xuaimian/AVPlayerViewController.git

看到網(wǎng)上很多都說,avplayer的layer沒有創(chuàng)建,所以無法加載頁面,估計是探路人一個人理解錯了,導(dǎo)致很多人理解錯了吧。其實不是這樣的,是avplayerviewcontroller沒有經(jīng)過viewdidappear 直接add,導(dǎo)致avplayerviewcontroller的view 不能獲得frame的大小,導(dǎo)致我們感覺沒有add上去,其實只要設(shè)置一下view的frame就好了。

方法一:

注意點:

1._controller要聲明成全局變量,不然無法控制播放。不聲明成全局變量,要想辦法retain一下,比如,self addchildcontrollers.

2.addobserver的時候,不要傳object,這時候傳遞的object是指的發(fā)送方,而不是參數(shù)。當(dāng)postnotification的對象與object不一致時,是不會響應(yīng)通知方法的。

_controller= [[AVPlayerViewController alloc]init];

AVPlayerItem *model =[[AVPlayerItem alloc]initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bhl912.mov" ofType:nil]]];

AVPlayer *player = [AVPlayer playerWithPlayerItem:model];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

[_controller setPlayer:player];

_controller.view.frame = self.view.bounds;

_controller.videoGravity = AVLayerVideoGravityResizeAspect;

_controller.showsPlaybackControls = YES;

[self.view addSubview:_controller.view];

[_controller.player play];

方法二


- (void)playeVideo

{

MoviePlayerViewController* avPlayer = [[MoviePlayerViewController alloc] init];

[avPlayer setName:@"bhl912.mov"];

[self presentViewController:avPlayer animated:YES completion:nil];

}


#import@interface MoviePlayerViewController : AVPlayerViewController

@property(nonatomic,strong)NSString *name ;

@end

@implementation MoviePlayerViewController

-(void)setName:(NSString *)name

{

_name = name;

NSString *playString = [[NSBundle mainBundle] pathForResource:name ofType:nil];

//視頻播放的url

NSURL *playerURL = [NSURL fileURLWithPath:playString];

self.player = [[AVPlayer alloc] initWithURL:playerURL];

self.videoGravity = AVLayerVideoGravityResizeAspect;

}

- (void)viewDidLoad {

[super viewDidLoad];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismiss) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

// Do any additional setup after loading the view.

}

-(void)dismiss

{

[self dismissViewControllerAnimated:YES completion:nil];

}

-(void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear: animated];

[self.player play];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)dealloc

{

[[NSNotificationCenter defaultCenter]removeObserver:self];

}

最后編輯于
?著作權(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)容