iphone - Resume AVPlayer video playback after app become active -
i write custom player avplayer video playback. according apple docs set video layer:
self.player = [iplplayer new]; self.player.playerlayer = (avplayerlayer *)self.playerview.layer;
where self.playerview usual class docs:
@implementation playerview + (class) layerclass { return [avplayerlayer class]; } - (avplayer *)player { return [(avplayerlayer *)[self layer] player]; } - (void)setplayer:(avplayer *) player { [(avplayerlayer *) [self layer] setplayer:player]; }
the problem is: when close app (home button), or block screen, video playback stopped, , when resume audio playback resumed, image on screen still before block screen - it's static , note change frames.
how resume video playing after screen blocked?
seems must register notifications, , after app become active resume video layer:
-(void)registernotification { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(willenterbackground) name:uiapplicationwillresignactivenotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(didenterforeground) name:uiapplicationdidbecomeactivenotification object:nil]; } -(void)unregisternotification { [[nsnotificationcenter defaultcenter] removeobserver:self]; } -(void)willenterbackground { nslog(@"willenterbackground"); [self.playerview willenterbackground]; } -(void)didenterforeground { nslog(@"didenterforeground"); [self.playerview didenterforeground]; }
and 1 solution binds information together.
maybe player status should handled differently, recursive way.
note: if not need exact seek time, can use [_player seektotime:<#(cmtime)#> completionhandler:<#^(bool finished)completionhandler#>]
it's faster seeks nearest key frame.
- (void)viewdidload { [super viewdidload]; .... [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(appenteredforeground) name:uiapplicationwillenterforegroundnotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(appenteredbackground) name:uiapplicationdidenterbackgroundnotification object:nil]; } -(void)viewwilldisappear:(bool)animated { [[nsnotificationcenter defaultcenter] removeobserver:self name:uiapplicationwillenterforegroundnotification object:nil]; [[nsnotificationcenter defaultcenter] removeobserver:self name:uiapplicationdidenterbackgroundnotification object:nil]; } .... -(void) appenteredforeground { avplayerlayer *player = (avplayerlayer *)[playerview layer]; [player setplayer:null]; [player setplayer:_player]; [self playat:currenttime]; } -(void) appenteredbackground { [_player pause]; currenttime = [_player currenttime]; } -(void)playat: (cmtime)time { if(_player.status == avplayerstatusreadytoplay && _player.currentitem.status == avplayeritemstatusreadytoplay) { [_player seektotime:time tolerancebefore:kcmtimezero toleranceafter:kcmtimezero completionhandler:^(bool finished) { [_player play]; }]; } else { dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(0.2 * nsec_per_sec)), dispatch_get_main_queue(), ^{ [self playat:time]; }); } }
Comments
Post a Comment