HMVideoPlayer.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // HMVideoPlayer.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2018/12/27.
  6. // Copyright © 2018 xfg. All rights reserved.
  7. //
  8. #import "HMVideoPlayer.h"
  9. @interface HMVideoPlayer ()<TXVodPlayListener>
  10. @property (nonatomic, assign) float duration;
  11. @property (nonatomic, assign) BOOL isNeedResume;
  12. @end
  13. @implementation HMVideoPlayer
  14. - (instancetype)init {
  15. if (self = [super init]) {
  16. // 监听APP退出后台及进入前台
  17. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  18. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
  19. }
  20. return self;
  21. }
  22. - (void)dealloc {
  23. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
  24. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
  25. }
  26. #pragma mark - Notification
  27. // APP退出到后台
  28. - (void)appDidEnterBackground:(NSNotification *)notify {
  29. if (self.status == HMVideoPlayerStatusLoading || self.status == HMVideoPlayerStatusPlaying) {
  30. [self pausePlay];
  31. self.isNeedResume = YES;
  32. }
  33. }
  34. // APP进入前台
  35. - (void)appWillEnterForeground:(NSNotification *)notify {
  36. if (self.isNeedResume && self.status == HMVideoPlayerStatusPaused) {
  37. self.isNeedResume = NO;
  38. [[AVAudioSession sharedInstance] setActive:YES error:nil];
  39. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  40. [self resumePlay];
  41. });
  42. }
  43. }
  44. #pragma mark - Public Methods
  45. - (void)playVideoWithView:(UIView *)playView url:(NSString *)url {
  46. // BOOL ret = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLoad"];
  47. // if (!ret) {
  48. // [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirstLoad"];
  49. // return;
  50. // }
  51. // 设置播放视图
  52. [self.player setupVideoWidget:playView insertIndex:0];
  53. // 准备播放
  54. [self playerStatusChanged:HMVideoPlayerStatusPrepared];
  55. // 开始播放
  56. if ([self.player startVodPlay:url] == 0) {
  57. // 这里可加入缓冲视图
  58. }else {
  59. [self playerStatusChanged:HMVideoPlayerStatusError];
  60. }
  61. }
  62. - (void)removeVideo {
  63. // 停止播放
  64. [self.player stopPlay];
  65. // 移除播放视图
  66. [self.player removeVideoWidget];
  67. // 改变状态
  68. [self playerStatusChanged:HMVideoPlayerStatusUnload];
  69. }
  70. - (void)pausePlay {
  71. [self playerStatusChanged:HMVideoPlayerStatusPaused];
  72. [self.player pause];
  73. }
  74. - (void)resumePlay {
  75. if (self.status == HMVideoPlayerStatusPaused) {
  76. [self.player resume];
  77. [self playerStatusChanged:HMVideoPlayerStatusPlaying];
  78. }
  79. }
  80. - (void)resetPlay {
  81. [self.player resume];
  82. [self playerStatusChanged:HMVideoPlayerStatusPlaying];
  83. }
  84. - (BOOL)isPlaying {
  85. return self.player.isPlaying;
  86. }
  87. #pragma mark - Private Methods
  88. - (void)playerStatusChanged:(HMVideoPlayerStatus)status {
  89. self.status = status;
  90. if ([self.delegate respondsToSelector:@selector(player:statusChanged:)]) {
  91. [self.delegate player:self statusChanged:status];
  92. }
  93. }
  94. #pragma mark - TXVodPlayListener
  95. - (void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary *)param {
  96. switch (EvtID) {
  97. case PLAY_EVT_PLAY_LOADING:{ // loading
  98. if (self.status == HMVideoPlayerStatusPaused) {
  99. [self playerStatusChanged:HMVideoPlayerStatusPaused];
  100. }else {
  101. [self playerStatusChanged:HMVideoPlayerStatusLoading];
  102. }
  103. }
  104. break;
  105. case PLAY_EVT_PLAY_BEGIN:{ // 开始播放
  106. [self playerStatusChanged:HMVideoPlayerStatusPlaying];
  107. }
  108. break;
  109. case PLAY_EVT_PLAY_END:{ // 播放结束
  110. if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) {
  111. [self.delegate player:self currentTime:self.duration totalTime:self.duration progress:1.0f];
  112. }
  113. [self playerStatusChanged:HMVideoPlayerStatusEnded];
  114. }
  115. break;
  116. case PLAY_ERR_NET_DISCONNECT:{ // 失败,多次重连无效
  117. [self playerStatusChanged:HMVideoPlayerStatusError];
  118. }
  119. break;
  120. case PLAY_EVT_PLAY_PROGRESS:{ // 进度
  121. if (self.status == HMVideoPlayerStatusPlaying) {
  122. self.duration = [param[EVT_PLAY_DURATION] floatValue];
  123. float currTime = [param[EVT_PLAY_PROGRESS] floatValue];
  124. float progress = self.duration == 0 ? 0 : currTime / self.duration;
  125. if ([self.delegate respondsToSelector:@selector(player:currentTime:totalTime:progress:)]) {
  126. [self.delegate player:self currentTime:currTime totalTime:self.duration progress:progress];
  127. }
  128. }
  129. }
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. - (void)onNetStatus:(TXVodPlayer *)player withParam:(NSDictionary *)param {
  136. }
  137. #pragma mark - 懒加载
  138. - (TXVodPlayer *)player {
  139. if (!_player) {
  140. [TXLiveBase setLogLevel:LOGLEVEL_NULL];
  141. [TXLiveBase setConsoleEnabled:NO];
  142. _player = [TXVodPlayer new];
  143. _player.vodDelegate = self;
  144. }
  145. return _player;
  146. }
  147. @end