| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- //
- // HMVideoPlayer.m
- // BuguLive
- //
- // Created by 范东 on 2018/12/27.
- // Copyright © 2018 xfg. All rights reserved.
- //
- #import "HMVideoPlayer.h"
- @interface HMVideoPlayer ()<TXVodPlayListener>
- @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
|