JXVideoPlayer.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. //
  2. // JXVideoPlayer.m
  3. // shiku_im
  4. //
  5. // Created by flyeagleTang on 17/1/12.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXVideoPlayer.h"
  9. #import "UIImage+Color.h"
  10. @implementation JXVideoPlayer
  11. @synthesize parent=_parent,videoFile=_videoFile,player=_player;
  12. - (id)initWithParent:(UIView*)value{
  13. self = [super init];
  14. if (self) {
  15. _player = nil;
  16. if (self.isShowHide == YES) { // 只能作聊天界面进入后的判断
  17. self.isScreenPlay = YES;
  18. }
  19. self.parent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, value.bounds.size.width, value.bounds.size.height)];
  20. self.parent.backgroundColor = [UIColor clearColor];
  21. [value addSubview:self.parent];
  22. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  23. [self.parent addGestureRecognizer:longPress];
  24. UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];
  25. screenPan.edges = UIRectEdgeLeft;
  26. [self.parent addGestureRecognizer:screenPan];
  27. // self.parent = value;
  28. self.isPlaying = NO;
  29. // 添加上下两个地方的透明模板
  30. _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, THE_DEVICE_HAVE_HEAD ? 62 : 42)];
  31. _botView = [[UIView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT-90, JX_SCREEN_WIDTH, 90)];
  32. [self setupView:_topView colors:@[(__bridge id)[[UIColor blackColor] colorWithAlphaComponent:0.6].CGColor,(__bridge id)[UIColor clearColor].CGColor]];
  33. [self setupView:_botView colors:@[(__bridge id)[UIColor clearColor].CGColor,(__bridge id)[[UIColor blackColor] colorWithAlphaComponent:0.6].CGColor]];
  34. _topView.hidden = YES;
  35. _botView.hidden = YES;
  36. [_parent addSubview:_topView];
  37. [_parent addSubview:_botView];
  38. [_parent bringSubviewToFront:_topView];
  39. [_parent bringSubviewToFront:_botView];
  40. _pauseBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, _botView.frame.size.height-57, 26, 26)];
  41. [_pauseBtn setBackgroundImage:[UIImage imageNamed:@"player_play"] forState:UIControlStateNormal];
  42. [_pauseBtn setBackgroundImage:[UIImage imageNamed:@"player_pause"] forState:UIControlStateSelected];
  43. [_pauseBtn addTarget:self action:@selector(switch) forControlEvents:UIControlEventTouchUpInside];
  44. _pauseBtn.hidden = YES;
  45. [_botView addSubview:_pauseBtn];
  46. // [_parent bringSubviewToFront:_pauseBtn];
  47. _timeLab = [[UILabel alloc] initWithFrame:CGRectMake(60, _botView.frame.size.height-52, 40, 13)];
  48. _timeLab.textAlignment = NSTextAlignmentCenter;
  49. _timeLab.font = [UIFont systemFontOfSize:10];
  50. _timeLab.textColor = [UIColor whiteColor];
  51. [_botView addSubview:_timeLab];
  52. // [_parent bringSubviewToFront:_timeLab];
  53. _timeEnd = [[UILabel alloc] initWithFrame:CGRectMake(_botView.frame.size.width-50, _botView.frame.size.height-52, 40, 13)];
  54. _timeEnd.textAlignment = NSTextAlignmentCenter;
  55. _timeEnd.font = [UIFont systemFontOfSize:10];
  56. _timeEnd.textColor = [UIColor whiteColor];
  57. [_botView addSubview:_timeEnd];
  58. // [_parent bringSubviewToFront:_timeEnd];
  59. // _outBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
  60. // [_outBtn setImage:[UIImage imageNamed:@"playvideo"] forState:UIControlStateNormal];
  61. // _outBtn.center = CGPointMake(_parent.frame.size.width/2,_parent.frame.size.height/2);
  62. // [_outBtn addTarget:self action:@selector(switch) forControlEvents:UIControlEventTouchUpInside];
  63. // if (self.isShowHide == YES) _outBtn.hidden = YES; // 只能作聊天界面进入后的判断
  64. // [_parent addSubview:_outBtn];
  65. // [_parent bringSubviewToFront:_outBtn];
  66. //进度条
  67. _movieTimeControl = [[UISlider alloc] initWithFrame:CGRectMake(100, _botView.frame.size.height-50, _botView.frame.size.width-160, 10)];
  68. _movieTimeControl.maximumTrackTintColor = [UIColor lightGrayColor];
  69. _movieTimeControl.minimumTrackTintColor = [UIColor whiteColor];
  70. _movieTimeControl.continuous = YES;
  71. _movieTimeControl.minimumValue = 0;
  72. _movieTimeControl.maximumValue = _timeLen;
  73. [_movieTimeControl setThumbImage:[UIImage scaleToSize:[UIImage imageNamed:@"circular"] size:CGSizeMake(14, 14)] forState:UIControlStateNormal];
  74. [_botView addSubview:_movieTimeControl];
  75. // [_parent bringSubviewToFront:_movieTimeControl];
  76. //显示第一帧图片
  77. _firstBaseView = [[UIView alloc] initWithFrame:_parent.bounds];
  78. _firstBaseView.backgroundColor = [UIColor blackColor];
  79. [_parent addSubview:_firstBaseView];
  80. CGRect frame = THE_DEVICE_HAVE_HEAD ? CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_TOP-JX_SCREEN_BOTTOM) : _firstBaseView.bounds;
  81. // CGRect frame = _firstBaseView.bounds;
  82. _videoFirst = [[UIImageView alloc] initWithFrame:frame];
  83. [_firstBaseView addSubview:_videoFirst];
  84. [FileInfo getFullFirstImageFromVideo:_videoFile imageView:_videoFirst];
  85. _wait = [[JXWaitView alloc] initWithParent:_firstBaseView];
  86. [_wait start];
  87. // [g_notify addObserver:self selector:@selector(playerStop:) name:kAllVideoPlayerStopNotifaction object:nil];//开始录音
  88. // [g_notify addObserver:self selector:@selector(playerPause:) name:kAllVideoPlayerPauseNotifaction object:nil];//开始录音
  89. [g_notify addObserver:self selector:@selector(EnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
  90. [g_notify addObserver:self selector:@selector(EnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
  91. [self performSelector:@selector(firstBaseViewAddTap) withObject:nil afterDelay:1];
  92. }
  93. return self;
  94. }
  95. - (void)firstBaseViewAddTap {
  96. if (_firstBaseView) {
  97. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstViewAction:)];
  98. [_firstBaseView addGestureRecognizer:tap];
  99. }
  100. }
  101. - (void)firstViewAction:(UITapGestureRecognizer *)tap {
  102. if (self.type != JXVideoTypePreview) { // 如果不是预览界面
  103. [[UIApplication sharedApplication] setStatusBarHidden:NO];
  104. }
  105. #ifdef IS_SHOW_MENU
  106. #else
  107. g_mainVC.bottomView.hidden = NO;
  108. #endif
  109. [_topView removeFromSuperview];
  110. [_botView removeFromSuperview];
  111. [_parent removeFromSuperview];
  112. [_videoFirst removeFromSuperview];
  113. [_firstBaseView removeFromSuperview];
  114. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  115. [self stop];
  116. [_player.view removeFromSuperview];
  117. _player.view = nil;
  118. _player = nil;
  119. });
  120. _parent = nil;
  121. }
  122. // 进入后台
  123. -(void)EnterBackground{
  124. if(_player.isOpened){
  125. // 调用暂停
  126. [self pause];
  127. }
  128. }
  129. //进入前台
  130. -(void)EnterForeground{
  131. if(_player.isOpened){
  132. //播放
  133. [self play];
  134. }
  135. }
  136. // 长按
  137. - (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer {
  138. if (self.isReadDel) {
  139. return;
  140. }
  141. JXActionSheetVC *actionVC = [[JXActionSheetVC alloc] initWithImages:@[] names:@[Localized(@"JX_SaveVideo")]];
  142. actionVC.delegate = self;
  143. [g_navigation.subViews.lastObject presentViewController:actionVC animated:NO completion:nil];
  144. }
  145. -(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan
  146. {
  147. }
  148. - (void)actionSheet:(JXActionSheetVC *)actionSheet didButtonWithIndex:(NSInteger)index {
  149. if ([_videoFile rangeOfString:@"http"].location != NSNotFound) {
  150. [self playerDownload:_videoFile];
  151. }else {
  152. [self saveVideo:_videoFile];
  153. }
  154. }
  155. //-----下载视频--
  156. - (void)playerDownload:(NSString *)url{
  157. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  158. NSString *documentsDirectory = [paths objectAtIndex:0];
  159. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  160. NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"jaibaili.mp4"];
  161. NSURL *urlNew = [NSURL URLWithString:url];
  162. NSURLRequest *request = [NSURLRequest requestWithURL:urlNew];
  163. NSURLSessionDownloadTask *task =
  164. [manager downloadTaskWithRequest:request
  165. progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  166. return [NSURL fileURLWithPath:fullPath];
  167. }
  168. completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  169. [self saveVideo:fullPath];
  170. }];
  171. [task resume];
  172. }
  173. //videoPath为视频下载到本地之后的本地路径
  174. - (void)saveVideo:(NSString *)videoPath{
  175. if (videoPath) {
  176. NSURL *url = [NSURL URLWithString:videoPath];
  177. BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]);
  178. if (compatible)
  179. {
  180. //保存相册核心代码
  181. UISaveVideoAtPathToSavedPhotosAlbum([url path], self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
  182. }
  183. }
  184. }
  185. //保存视频完成之后的回调
  186. - (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
  187. if (error) {
  188. NSLog(@"保存视频失败%@", error.localizedDescription);
  189. [g_server showMsg:Localized(@"JX_SaveFiled") delay:.5];
  190. }
  191. else {
  192. NSLog(@"保存视频成功");
  193. [g_server showMsg:Localized(@"JX_SaveSuessed") delay:.5];
  194. }
  195. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  196. NSString *documentsDirectory = [paths objectAtIndex:0];
  197. NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"jaibaili.mp4"];
  198. [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
  199. }
  200. - (void)dealloc {
  201. NSLog(@"JXVideoPlayer.dealloc");
  202. self.parent = nil;
  203. self.videoFile = nil;
  204. // [_pauseBtn release];
  205. [g_notify removeObserver:self];
  206. [self stop];
  207. // [super dealloc];
  208. }
  209. - (void)exitVideoPlayer {
  210. if(self.delegate != nil && [self.delegate respondsToSelector:self.didExitBtn])
  211. [self.delegate performSelectorOnMainThread:self.didExitBtn withObject:self waitUntilDone:NO];
  212. [self doPlayEnd];
  213. [self stop];
  214. if (self.type != JXVideoTypePreview) { // 如果不是预览界面
  215. [[UIApplication sharedApplication] setStatusBarHidden:NO];
  216. }
  217. #ifdef IS_SHOW_MENU
  218. #else
  219. g_mainVC.bottomView.hidden = NO;
  220. #endif
  221. [_topView removeFromSuperview];
  222. [_botView removeFromSuperview];
  223. [_parent removeFromSuperview];
  224. [_videoFirst removeFromSuperview];
  225. [_firstBaseView removeFromSuperview];
  226. [_player.view removeFromSuperview];
  227. _player.view = nil;
  228. _parent = nil;
  229. }
  230. -(void)stop{
  231. if(_player == nil)
  232. return;
  233. [_player stop];
  234. [_player.view removeFromSuperview];
  235. // [_player release];
  236. _player = nil;
  237. }
  238. - (void)switch{
  239. #ifdef IS_SHOW_MENU
  240. #else
  241. g_mainVC.bottomView.hidden = YES;
  242. #endif
  243. _pauseBtn.hidden = NO;
  244. if(_player.isOpened){
  245. if(_player.isPlaying)
  246. [self pause];
  247. else
  248. [self play];
  249. }else{
  250. [self start];
  251. [self play];
  252. }
  253. }
  254. -(void)play{
  255. [g_notify postNotificationName:kAllVideoPlayerPauseNotifaction object:self userInfo:nil];
  256. [g_notify postNotificationName:kAllAudioPlayerPauseNotifaction object:self userInfo:nil];
  257. if (self.isShowHide) { // 聊天界面全屏播放隐藏状态栏
  258. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  259. }
  260. if (self.isStartFullScreenPlay) {
  261. [self actionFullScreen];
  262. }
  263. if(_player.isOpened){
  264. if(!_player.isPlaying){
  265. [_player play:nil];
  266. [self doPlayBegin];
  267. }
  268. }
  269. _exitBtn.hidden = NO;
  270. }
  271. -(void)pause{
  272. if(_player.isOpened)
  273. if(_player.isPlaying){
  274. [_player pause:nil];
  275. [self doPause];
  276. }
  277. }
  278. //显示播放器
  279. - (void)start{
  280. _player = [[JXVideoPlayerVC alloc] init];
  281. _player.isVideo = self.isVideo;
  282. _player.pauseButton = _pauseBtn;
  283. _player.delegate = self;
  284. _player.parent = _parent;
  285. _player.timeCur = _timeLab;
  286. _player.timeEnd = _timeEnd;
  287. _player.movieTimeControl = _movieTimeControl;
  288. _player.didClick = @selector(onClickVideo:);
  289. _player.didPlayNext = @selector(didPlayEnd);
  290. _player.didOpen = @selector(doVideoOpen);
  291. NSString *filePath = [NSString stringWithFormat:@"%@%@",myTempFilePath,[self.videoFile lastPathComponent]];
  292. if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
  293. [_player open:filePath];
  294. else
  295. [_player open:self.videoFile];
  296. [_player setFrame:_parent.bounds];
  297. // 添加左上角叉
  298. _exitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  299. [_exitBtn addTarget:self action:@selector(exitVideoPlayer) forControlEvents:UIControlEventTouchUpInside];
  300. if (!self.isPlaying) _exitBtn.hidden = YES; // 如果没在播放就隐藏
  301. _exitBtn.hidden = YES;
  302. [_topView addSubview:_exitBtn];
  303. [_topView bringSubviewToFront:_exitBtn];
  304. UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(25, 25, 16, 16)];
  305. imgV.image = [UIImage imageNamed:@"fork_white"];
  306. [_exitBtn addSubview:imgV];
  307. _timeEnd.text = [self formatTime:_timeLen];
  308. if (_isPreview) { // 预览界面处理
  309. _timeEnd.hidden = YES;
  310. _player.movieTimeControl.hidden = YES;
  311. _pauseBtn.hidden = YES;
  312. _exitBtn.hidden = YES;
  313. _timeLab.hidden = YES;
  314. _exitBtn.hidden = YES;
  315. _disBtn = [self createButtonWithFrame:CGRectMake(20, JX_SCREEN_HEIGHT-20-80, 80, 80) image:@"video_return" action:@selector(exitVideoPlayer)];
  316. _sendBtn = [self createButtonWithFrame:CGRectMake(JX_SCREEN_WIDTH-20-80, JX_SCREEN_HEIGHT-20-80, 80, 80) image:@"video_gou" action:@selector(didButtonWithSendVideo)];
  317. [_player.view addSubview:_disBtn];
  318. [_player.view addSubview:_sendBtn];
  319. }
  320. }
  321. //播放视频被点击
  322. - (void)onClickVideo:(id)sender{
  323. if (self.type == JXVideoTypePreview) {
  324. return;
  325. }
  326. _topView.hidden = !_topView.hidden;
  327. _botView.hidden = !_botView.hidden;
  328. // if (_player.isPlaying && _isShowHide == NO) {
  329. // [self actionFullScreen];
  330. // }
  331. }
  332. - (void)dismissVideoPlayer {
  333. if (self.type == JXVideoTypeChat) {
  334. _topView.hidden = NO;
  335. _botView.hidden = NO;
  336. }
  337. }
  338. - (void)didPlayEnd{
  339. self.isEndPlay = YES;
  340. if (!_isPreview) {
  341. [self dismissVideoPlayer];
  342. }
  343. [self doPlayEnd];
  344. if (_player.isFullScreen) {
  345. [self actionFullScreen];
  346. }
  347. }
  348. //全屏播放
  349. - (void)actionFullScreen{
  350. if (_isShowHide == YES) {
  351. return;
  352. }
  353. if (_player.isVideo) {
  354. if (_player.isFullScreen) {
  355. // [_player setFrame:_parent.bounds];
  356. // [_parent addSubview:_player.view];
  357. // [self adjust];
  358. }else{
  359. [_player setFrame:g_window.bounds];
  360. [self.parent addSubview:_player.view];
  361. [self.parent bringSubviewToFront:_player.view];
  362. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  363. [_player.view addSubview:_topView];
  364. [_player.view addSubview:_botView];
  365. [_exitBtn addTarget:self action:@selector(nowExitVideoPlayer) forControlEvents:UIControlEventTouchUpInside];
  366. }
  367. _player.isFullScreen = !_player.isFullScreen;
  368. // [_player setSliderHidden:_player.isFullScreen];
  369. [_player set90];
  370. }
  371. }
  372. - (void)nowExitVideoPlayer {
  373. _isEndPlay = NO;
  374. [[UIApplication sharedApplication] setStatusBarHidden:NO];
  375. [_player stop];
  376. [_player setFrame:_parent.bounds];
  377. [_parent addSubview:_player.view];
  378. [self adjust];
  379. _exitBtn.hidden = YES;
  380. }
  381. -(void)playerStop:(NSNotification*)notification{
  382. if([notification.object isEqual:self])
  383. return;
  384. [self stop];
  385. }
  386. -(void)playerPause:(NSNotification*)notification{
  387. if([notification.object isEqual:self])
  388. return;
  389. [self pause];
  390. }
  391. -(void)adjust{
  392. [_player.view removeFromSuperview];
  393. if(_parent==nil)
  394. return;
  395. [_parent addSubview:_player.view];
  396. [_player setFrame:_parent.bounds];
  397. // if (_isScreenPlay == NO || !_isStartFullScreenPlay) {
  398. // _outBtn.center = CGPointMake(_parent.frame.size.width/2,_parent.frame.size.height/2);
  399. // }
  400. [_parent addSubview:_topView];
  401. [_parent addSubview:_botView];
  402. [_parent bringSubviewToFront:_topView];
  403. [_parent bringSubviewToFront:_botView];
  404. // [_parent addSubview:_outBtn];
  405. // [_parent bringSubviewToFront:_outBtn];
  406. //
  407. // [_parent addSubview:_pauseBtn];
  408. // [_parent bringSubviewToFront:_pauseBtn];
  409. //
  410. // [_parent addSubview:_timeLab];
  411. // [_parent bringSubviewToFront:_timeEnd];
  412. //
  413. // [_parent addSubview:_timeEnd];
  414. // [_parent bringSubviewToFront:_timeLab];
  415. //
  416. // [_parent addSubview:_movieTimeControl];
  417. // [_parent bringSubviewToFront:_movieTimeControl];
  418. }
  419. -(void)setParent:(UIView *)value{
  420. [self adjust];
  421. if([_parent isEqual:value])
  422. return;
  423. // [_parent release];
  424. // _parent = [value retain];
  425. _parent = value;
  426. [self adjust];
  427. }
  428. -(void)setVideoFile:(NSString *)value{
  429. [self adjust];
  430. [_player stop];
  431. if([_videoFile isEqual:value])
  432. return;
  433. NSLog(@"vidoe:%@",value);
  434. // [_videoFile release];
  435. // _videoFile = [value retain];
  436. _videoFile = value;
  437. NSString* s = [_videoFile lowercaseString];
  438. self.isVideo = [s rangeOfString:@".mp4"].location != NSNotFound
  439. || [s rangeOfString:@".qt"].location != NSNotFound
  440. || [s rangeOfString:@".mpg"].location != NSNotFound
  441. || [s rangeOfString:@".mov"].location != NSNotFound
  442. || [s rangeOfString:@".avi"].location != NSNotFound;
  443. }
  444. -(void)setIsVideo:(BOOL)value{
  445. _isVideo = value;
  446. if(self.isVideo){
  447. [_pauseBtn setBackgroundImage:[UIImage imageNamed:@"player_play"] forState:UIControlStateNormal];
  448. [_pauseBtn setBackgroundImage:[UIImage imageNamed:@"player_pause"] forState:UIControlStateSelected];
  449. }else{
  450. [_pauseBtn setBackgroundImage:[UIImage imageNamed:@"feeds_play_btn_u"] forState:UIControlStateNormal];
  451. [_pauseBtn setBackgroundImage:[UIImage imageNamed:@"feeds_play_btn_h_u"] forState:UIControlStateSelected];
  452. }
  453. }
  454. -(void)doVideoOpen{
  455. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  456. [self adjust];
  457. [_wait stop];
  458. });
  459. self.isPlaying = NO;
  460. if(self.delegate != nil && [self.delegate respondsToSelector:self.didVideoOpen])
  461. [self.delegate performSelectorOnMainThread:self.didVideoOpen withObject:self waitUntilDone:NO];
  462. if (_isPreview) _pauseBtn.hidden = YES; //预览界面 隐藏暂停按钮
  463. }
  464. -(void)doPlayEnd{
  465. if (_isPreview || self.type == JXVideoTypeWeibo) {
  466. [self switch];
  467. return;
  468. }
  469. self.isPlaying = NO;
  470. if(self.delegate != nil && [self.delegate respondsToSelector:self.didVideoPlayEnd])
  471. [self.delegate performSelectorOnMainThread:self.didVideoPlayEnd withObject:self waitUntilDone:NO];
  472. }
  473. -(void)doPlayBegin{
  474. self.isPlaying = YES;
  475. if(self.delegate != nil && [self.delegate respondsToSelector:self.didVideoPlayBegin])
  476. [self.delegate performSelectorOnMainThread:self.didVideoPlayBegin withObject:self waitUntilDone:NO];
  477. }
  478. -(void)doPause{
  479. self.isPlaying = NO;
  480. if(self.delegate != nil && [self.delegate respondsToSelector:self.didVideoPause])
  481. [self.delegate performSelectorOnMainThread:self.didVideoPause withObject:self waitUntilDone:NO];
  482. }
  483. -(void)setHidden:(BOOL)value{
  484. _pauseBtn.hidden = value;
  485. _timeLab.hidden = value;
  486. }
  487. -(void)setTimeLen:(int)value{
  488. _timeLen = value;
  489. _timeLab.text = [self formatTime:value];
  490. }
  491. - (NSString *) formatTime:(NSTimeInterval)num
  492. {
  493. int n = num;
  494. int secs = n % 60;
  495. int min = n / 60;
  496. if (num < 60) return [NSString stringWithFormat:@"0:%02d", n];
  497. return [NSString stringWithFormat:@"%d:%02d", min, secs];
  498. }
  499. - (void)didButtonWithSendVideo {
  500. if (self.delegate && [self.delegate respondsToSelector:self.didSendBtn]) {
  501. [_player stop];
  502. [self.delegate performSelectorOnMainThread:self.didSendBtn withObject:self waitUntilDone:NO];
  503. }
  504. }
  505. - (UIButton *)createButtonWithFrame:(CGRect)frame image:(NSString *)image action:(SEL)action {
  506. UIButton *button = [[UIButton alloc] initWithFrame:frame];
  507. button.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
  508. button.layer.masksToBounds = YES;
  509. button.layer.cornerRadius = button.frame.size.width/2;
  510. [button setImage:[UIImage scaleToSize:[UIImage imageNamed:image] size:CGSizeMake(32, 32)] forState:UIControlStateNormal];
  511. [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
  512. return button;
  513. }
  514. - (void)setupView:(UIView *)view colors:(NSArray *)colors {
  515. CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  516. gradientLayer.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, 100); // 设置显示的frame
  517. gradientLayer.colors = colors; // 设置渐变颜色
  518. gradientLayer.startPoint = CGPointMake(0, 0);
  519. gradientLayer.endPoint = CGPointMake(0, 1);
  520. [view.layer addSublayer:gradientLayer];
  521. }
  522. @end