- (void)viewDidLoad {
[super viewDidLoad];
[self setupForAVplayerView];
}
- (void)setupForAVplayerView
{
AVPlayerLayer*playerLayer = [AVPlayerLayerplayerLayerWithPlayer:self.player];
playerLayer.frame=CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layeraddSublayer:playerLayer];
///登录按钮
UIButton* loginBtn =[[UIButtonalloc]init];
loginBtn.frame=CGRectMake(0,self.view.bottom-200,241,54);
loginBtn.centerX =self.view.centerX;
[loginBtnsetImage:[UIImage imageNamed:@"login_btn"] forState:UIControlStateNormal];
[self.viewaddSubview:loginBtn];
[loginBtnaddTarget:selfaction:@selector(loginBtnBtnClick)forControlEvents:UIControlEventTouchUpInside];
///
}
/**
* 初始化播放器
*/
- (AVPlayer *)player
{
if(!_player) {
AVPlayerItem*playerItem = [selfgetPlayItem];
_player= [AVPlayerplayerWithPlayerItem:playerItem];
[_playerplay];
//设置重复播放
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone; // set this
//视频播放完发通知
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(__playerItemDidPlayToEndTimeNotification:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
}
return _player;
}
- (AVPlayerItem *)getPlayItem
{
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"loginvideo" ofType:@"mp4"];
NSURL*url = [NSURLfileURLWithPath:filePath];
AVPlayerItem*playerItem = [AVPlayerItemplayerItemWithURL:url];
returnplayerItem;
}
- (void)__playerItemDidPlayToEndTimeNotification:(NSNotification *)sender
{
[_player seekToTime:kCMTimeZero]; // 设置从头继续播放
}