// // HMVideoPlayer.m // BuguLive // // Created by 范东 on 2018/12/27. // Copyright © 2018 xfg. All rights reserved. // #import "HMVideoPlayer.h" @interface HMVideoPlayer () @property (nonatomic, assign) float duration; @property (nonatomic, assign) BOOL isNeedResume; @end @implementation HMVideoPlayer - (instancetype)init { if (self = [super init]) { // 监听APP退出后台及进入前台 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; } return self; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; } #pragma mark - Notification // APP退出到后台 - (void)appDidEnterBackground:(NSNotification *)notify { if (self.status == HMVideoPlayerStatusLoading || self.status == HMVideoPlayerStatusPlaying) { [self pausePlay]; self.isNeedResume = YES; } } // APP进入前台 - (void)appWillEnterForeground:(NSNotification *)notify { if (self.isNeedResume && self.status == HMVideoPlayerStatusPaused) { self.isNeedResume = NO; [[AVAudioSession sharedInstance] setActive:YES error:nil]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self resumePlay]; }); } } #pragma mark - Public Methods - (void)playVideoWithView:(UIView *)playView url:(NSString *)url { // BOOL ret = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLoad"]; // if (!ret) { // [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirstLoad"]; // return; // } // 设置播放视图 [self.player setupVideoWidget:playView insertIndex:0]; // 准备播放 [self playerStatusChanged:HMVideoPlayerStatusPrepared]; // 开始播放 if ([self.player startVodPlay:url] == 0) { // 这里可加入缓冲视图 }else { [self playerStatusChanged:HMVideoPlayerStatusError]; } } - (void)removeVideo { // 停止播放 [self.player stopPlay]; // 移除播放视图 [self.player removeVideoWidget]; // 改变状态 [self playerStatusChanged:HMVideoPlayerStatusUnload]; } - (void)pausePlay { [self playerStatusChanged:HMVideoPlayerStatusPaused]; [self.player pause]; } - (void)resumePlay { if (self.status == HMVideoPlayerStatusPaused) { [self.player resume]; [self playerStatusChanged:HMVideoPlayerStatusPlaying]; } } - (void)resetPlay { [self.player resume]; [self playerStatusChanged:HMVideoPlayerStatusPlaying]; } - (BOOL)isPlaying { return self.player.isPlaying; } #pragma mark - Private Methods - (void)playerStatusChanged:(HMVideoPlayerStatus)status { self.status = status; if ([self.delegate respondsToSelector:@selector(player:statusChanged:)]) { [self.delegate player:self statusChanged:status]; } } #pragma mark - TXVodPlayListener - (void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary *)param { switch (EvtID) { case PLAY_EVT_PLAY_LOADING:{ // loading if (self.status == HMVideoPlayerStatusPaused) { [self playerStatusChanged:HMVideoPlayerStatusPaused]; }else { [self playerStatusChanged:HMVideoPlayerStatusLoading]; } } break; case PLAY_EVT_PLAY_BEGIN:{ // 开始播放 [self playerStatusChanged:HMVideoPlayerStatusPlaying]; } break; case PLAY_EVT_PLAY_END:{ // 播放结束 if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) { [self.delegate player:self currentTime:self.duration totalTime:self.duration progress:1.0f]; } [self playerStatusChanged:HMVideoPlayerStatusEnded]; } break; case PLAY_ERR_NET_DISCONNECT:{ // 失败,多次重连无效 [self playerStatusChanged:HMVideoPlayerStatusError]; } break; case PLAY_EVT_PLAY_PROGRESS:{ // 进度 if (self.status == HMVideoPlayerStatusPlaying) { self.duration = [param[EVT_PLAY_DURATION] floatValue]; float currTime = [param[EVT_PLAY_PROGRESS] floatValue]; float progress = self.duration == 0 ? 0 : currTime / self.duration; if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) { [self.delegate player:self currentTime:currTime totalTime:self.duration progress:progress]; } } } break; default: break; } } - (void)onNetStatus:(TXVodPlayer *)player withParam:(NSDictionary *)param { } #pragma mark - 懒加载 - (TXVodPlayer *)player { if (!_player) { [TXLiveBase setLogLevel:LOGLEVEL_NULL]; [TXLiveBase setConsoleEnabled:NO]; _player = [TXVodPlayer new]; _player.vodDelegate = self; } return _player; } @end