XMPlayerVC.mm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // XMPlayerVC.m
  3. // XWorld_General
  4. //
  5. // Created by SaturdayNight on 2018/8/6.
  6. // Copyright © 2018年 xiongmaitech. All rights reserved.
  7. //
  8. #import "XMPlayerVC.h"
  9. #import <FunSDK/FunSDK.h>
  10. #import "CYGLKView.h"
  11. #import "Header.h"
  12. @interface XMPlayerVC ()
  13. @property (nonatomic,strong) UIButton *btnBack; // 返回按钮
  14. @property (nonatomic,strong) UILabel *lbBackTitle; // 标题
  15. @property (nonatomic,strong) UIView *playerMenu; // 播放菜单
  16. @property (nonatomic,strong) UISlider *sliderVideo; // 进度条
  17. @property (nonatomic,strong) UILabel *lbTimeLeft;
  18. @property (nonatomic,strong) UILabel *lbTimeRight;
  19. @property (nonatomic,strong) UIButton *btnPlay;
  20. @property (nonatomic,assign) BOOL isPause; // 是否暂停
  21. @property (nonatomic,assign) int startTime; // 录像开始时间
  22. @property (nonatomic,strong) CYGLKView *glView; // 视频显示
  23. @property (strong, nonatomic) EAGLContext *context;
  24. @property (nonatomic,assign) int msgHandle;
  25. @property (nonatomic,assign) FUN_HANDLE player; // 播放器句柄
  26. @end
  27. @implementation XMPlayerVC
  28. -(instancetype)init{
  29. self= [super init];
  30. self.msgHandle = FUN_RegWnd((__bridge LP_WND_OBJ)self);
  31. return self;
  32. }
  33. -(void)dealloc{
  34. FUN_UnRegWnd(self.msgHandle);
  35. self.msgHandle = -1;
  36. }
  37. - (void)viewDidLoad{
  38. [super viewDidLoad];
  39. self.view.backgroundColor = [UIColor blackColor];
  40. [self.view addSubview:self.glView];
  41. [self.view addSubview:self.btnBack];
  42. [self.view addSubview:self.lbBackTitle];
  43. [self.view addSubview:self.playerMenu];
  44. [self.view addSubview:self.sliderVideo];
  45. [self.view addSubview:self.lbTimeLeft];
  46. [self.view addSubview:self.lbTimeRight];
  47. [self.view addSubview:self.btnPlay];
  48. [self myLayout];
  49. [self playVideo];
  50. }
  51. #pragma mark - EventAction
  52. -(void)btnBackClicked{
  53. FUN_MediaSetSound(self.player, 0, 0);
  54. FUN_MediaStop(self.player);
  55. [self dismissViewControllerAnimated:YES completion:nil];
  56. }
  57. #pragma mark - 播放/暂停按钮点击事件
  58. - (void)playBtnClicked:(UIButton *)sender
  59. {
  60. sender.selected = !sender.selected;
  61. if (self.btnPlay.selected == NO && self.isPause == NO) {
  62. [self playVideo];
  63. }
  64. else{
  65. [self pauseOrResumePlay];
  66. }
  67. }
  68. #pragma mark - 开始播放
  69. -(void)playVideo
  70. {
  71. self.player = FUN_MediaLocRecordPlay(self.msgHandle, [self.filePath UTF8String] , (__bridge void*)(self.glView),0);
  72. FUN_MediaSetSound(self.player, 100, 0);
  73. }
  74. #pragma mark - UIControlEventValueChanged
  75. -(void)sliderValueChanged:(UISlider *)slider {
  76. self.lbTimeLeft.text = [self changeSecToTimeText:slider.value];
  77. }
  78. -(void)sliderTouchDown:(UISlider *)slider {
  79. [self pauseOrResumePlay];
  80. }
  81. - (void)sliderAction:(UISlider *)slider
  82. {
  83. [self pauseOrResumePlay];
  84. FUN_MediaSeekToTime(self.player,self.sliderVideo.value, 0, 0);
  85. }
  86. #pragma mark - 暂停恢复播放
  87. - (void)pauseOrResumePlay
  88. {
  89. self.isPause = !self.isPause;
  90. FUN_MediaPause(self.player,-1);
  91. }
  92. #pragma mark - 将秒转化成显示的时间
  93. - (NSString *)changeSecToTimeText:(int)second
  94. {
  95. NSString *timeStr = @"";
  96. int min = (int)second/60;
  97. NSString *minStr;
  98. if (min < 10) {
  99. minStr = [NSString stringWithFormat:@"0%d",min];
  100. }
  101. else{
  102. minStr = [NSString stringWithFormat:@"%d",min];
  103. }
  104. int sec = second- ((int)second/60) * 60;
  105. NSString *secStr;
  106. if (sec < 10) {
  107. secStr = [NSString stringWithFormat:@"0%d",sec];
  108. }
  109. else{
  110. secStr = [NSString stringWithFormat:@"%d",sec];
  111. }
  112. timeStr = [NSString stringWithFormat:@"%@:%@",minStr,secStr];
  113. return timeStr;
  114. }
  115. #pragma mark - FunSDK CallBack
  116. -(void)OnFunSDKResult:(NSNumber *) pParam
  117. {
  118. NSInteger nAddr = [pParam integerValue];
  119. MsgContent *msg = (MsgContent *)nAddr;
  120. switch (msg->id) {
  121. case EMSG_START_PLAY://开始播放回
  122. {
  123. int time = msg->param3 - msg->param2 + 1;
  124. self.startTime = msg->param2;
  125. self.lbTimeRight.text = [NSString stringWithFormat:@"%@",[self changeSecToTimeText:time]];
  126. self.sliderVideo.maximumValue = time;
  127. NSLog(@"开始\np1------%d\np2------%d\np3------%d",msg->param1,msg->param2,msg->param3);
  128. }
  129. break;
  130. case EMSG_ON_PLAY_INFO: //收到解码信息回调
  131. {
  132. int playTime = msg->param2 - msg->param1;
  133. [self.sliderVideo setValue:playTime animated:NO];
  134. self.lbTimeLeft.text = [self changeSecToTimeText:playTime];
  135. NSLog(@"播放\np1------%d\np2------%d\np3------%d",msg->param1,msg->param2,msg->param3);
  136. }
  137. break;
  138. case EMSG_ON_PLAY_END: // 录像播放结束
  139. {
  140. self.btnPlay.selected = !self.btnPlay.selected;
  141. self.lbTimeLeft.text = @"00:00";
  142. [self.sliderVideo setValue:0 animated:NO];
  143. FUN_MediaStop(self.player);
  144. NSLog(@"结束\np1------%d\np2------%d\np3------%d",msg->param1,msg->param2,msg->param3);
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. -(void)myLayout{
  152. self.btnBack.frame = CGRectMake(10, 15, 45, 40);
  153. self.lbBackTitle.frame = CGRectMake(70, 15, ScreenWidth - 140, 40);
  154. self.glView.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth * 0.75);
  155. self.glView.center = self.view.center;
  156. self.playerMenu.frame = CGRectMake(10, ScreenHeight -80, ScreenWidth - 20, 70);
  157. self.sliderVideo.frame = CGRectMake(30,ScreenHeight - 60, ScreenWidth - 60, 10);
  158. self.lbTimeLeft.frame = CGRectMake(20, ScreenHeight - 50, 100, 30);
  159. self.lbTimeRight.frame = CGRectMake(ScreenWidth -120, ScreenHeight - 50, 100, 30);
  160. self.btnPlay.frame = CGRectMake(ScreenWidth/2 - 20, ScreenHeight - 50, 30, 30);
  161. }
  162. #pragma mark - LazyLoad
  163. -(CYGLKView *)glView{
  164. if (!_glView) {
  165. _glView = [[CYGLKView alloc] init];
  166. _glView.backgroundColor = [UIColor blackColor];
  167. }
  168. return _glView;
  169. }
  170. -(UIButton *)btnBack{
  171. if (!_btnBack) {
  172. _btnBack = [[UIButton alloc] init];
  173. [_btnBack setBackgroundImage:[UIImage imageNamed:@"new_back.png"] forState:UIControlStateNormal];
  174. [_btnBack addTarget:self action:@selector(btnBackClicked) forControlEvents:UIControlEventTouchUpInside];
  175. }
  176. return _btnBack;
  177. }
  178. -(UILabel *)lbBackTitle{
  179. if (!_lbBackTitle) {
  180. _lbBackTitle = [[UILabel alloc] init];
  181. _lbBackTitle.textColor = [UIColor whiteColor];
  182. _lbBackTitle.textAlignment = NSTextAlignmentCenter;
  183. _lbBackTitle.text = [self.filePath lastPathComponent];
  184. }
  185. return _lbBackTitle;
  186. }
  187. -(UIView *)playerMenu{
  188. if (!_playerMenu) {
  189. _playerMenu = [[UIView alloc] init];
  190. _playerMenu.backgroundColor = [UIColor colorWithRed:80/255.0 green:80/255.0 blue:80/255.0 alpha:1];
  191. _playerMenu.layer.cornerRadius = 5;
  192. _playerMenu.layer.masksToBounds = YES;
  193. }
  194. return _playerMenu;
  195. }
  196. -(UISlider *)sliderVideo{
  197. if (!_sliderVideo) {
  198. _sliderVideo = [[UISlider alloc] init];
  199. _sliderVideo.minimumTrackTintColor = [UIColor redColor];
  200. _sliderVideo.maximumTrackTintColor = [UIColor colorWithRed:68/255.0 green:71/255.0 blue:72/255.0 alpha:1];
  201. _sliderVideo.maximumValue = 30 * 60;
  202. [_sliderVideo addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
  203. [_sliderVideo addTarget:self action:@selector(sliderTouchDown:) forControlEvents:UIControlEventTouchDown];
  204. [_sliderVideo addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  205. [_sliderVideo addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpOutside];
  206. }
  207. return _sliderVideo;
  208. }
  209. -(UILabel *)lbTimeLeft{
  210. if (!_lbTimeLeft) {
  211. _lbTimeLeft = [[UILabel alloc] init];
  212. _lbTimeLeft.textColor = [UIColor whiteColor];
  213. _lbTimeLeft.text = @"00:00";
  214. _lbTimeLeft.font = [UIFont systemFontOfSize:12];
  215. }
  216. return _lbTimeLeft;
  217. }
  218. -(UILabel *)lbTimeRight{
  219. if (!_lbTimeRight) {
  220. _lbTimeRight = [[UILabel alloc] init];
  221. _lbTimeRight.textColor = [UIColor whiteColor];
  222. _lbTimeRight.textAlignment = NSTextAlignmentRight;
  223. _lbTimeRight.text = @"00:00";
  224. _lbTimeRight.font = [UIFont systemFontOfSize:12];
  225. }
  226. return _lbTimeRight;
  227. }
  228. -(UIButton *)btnPlay{
  229. if (!_btnPlay) {
  230. _btnPlay = [[UIButton alloc] initWithFrame:CGRectZero];
  231. [_btnPlay addTarget:self action:@selector(playBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  232. [_btnPlay setImage:[UIImage imageNamed:@"fisheye_video_pouse"] forState:UIControlStateNormal];
  233. [_btnPlay setImage:[UIImage imageNamed:@"fisheye_video_play"] forState:UIControlStateSelected];
  234. }
  235. return _btnPlay;
  236. }
  237. @end