YHPlayerViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // YHPlayerViewController.m
  3. // BuguLive
  4. //
  5. // Created by 宋晨光 on 2019/8/29.
  6. // Copyright © 2019年 xfg. All rights reserved.
  7. //
  8. #import "YHPlayerViewController.h"
  9. #import <CLPlayer/CLPlayerView.h>
  10. @interface YHPlayerViewController ()
  11. @property (nonatomic, strong) CLPlayerView *playerView;
  12. @property(nonatomic, assign) BOOL isDisappear;
  13. @end
  14. @implementation YHPlayerViewController
  15. - (instancetype)initWithPlayerURL:(NSString *)url{
  16. YHPlayerViewController *vc = [YHPlayerViewController new];
  17. vc.url = url;
  18. return vc;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. [self setUpView];
  24. }
  25. -(void)setUpView{
  26. UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  27. bgView.backgroundColor = kBlackColor;
  28. [self.view addSubview:bgView];
  29. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  30. backBtn.frame = CGRectMake(10, kTopHeight - 20, 40, 30);
  31. [backBtn setImage:[UIImage imageNamed:@"ac_auction_back"] forState:UIControlStateNormal];
  32. [backBtn addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
  33. _playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  34. _playerView.backgroundColor = kClearColor;
  35. _playerView.maskView.playButton.alpha = 0;
  36. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPlayerView:)];
  37. [_playerView.maskView addGestureRecognizer:tap];
  38. // [_playerView.maskView.playButton addTarget:self action:@selector(clickPlayerView:) forControlEvents:UIControlEventTouchUpInside];
  39. _playerView.configure.mute = YES;
  40. _playerView.configure.repeatPlay = YES;
  41. _playerView.isFullScreen = YES;
  42. _playerView.configure.mute = NO;
  43. _playerView.configure.isLandscape = YES;
  44. __weak __typeof(self)weakSelf = self;
  45. [_playerView updateWithConfigure:^(CLPlayerViewConfigure *configure) {
  46. _playerView.url = [NSURL URLWithString:self.url];
  47. //播放
  48. [_playerView playVideo];
  49. }];
  50. //返回按钮点击事件回调
  51. [_playerView backButton:^(UIButton *button) {
  52. NSLog(ASLocalizedString(@"返回按钮被点击"));
  53. }];
  54. //播放完成回调
  55. [_playerView endPlay:^{
  56. }];
  57. [bgView addSubview:_playerView];
  58. [bgView addSubview:backBtn];
  59. }
  60. -(void)clickPlayerView:(UIButton *)sender{
  61. if (_playerView.isUserPlay) {
  62. [_playerView pausePlay];
  63. }else{
  64. [_playerView playVideo];
  65. }
  66. _playerView.isUserPlay = !_playerView.isUserPlay;
  67. _playerView.maskView.playButton.selected = _playerView.isUserPlay;
  68. if (_isDisappear){
  69. [UIView animateWithDuration:0.5 animations:^{
  70. _playerView.maskView.playButton.alpha = 0;
  71. }];
  72. }else{
  73. //重置定时消失
  74. [UIView animateWithDuration:0.5 animations:^{
  75. _playerView.maskView.playButton.alpha = 1;
  76. }];
  77. }
  78. _isDisappear = !_isDisappear;
  79. }
  80. -(void)clickBack:(UIButton *)sender{
  81. [_playerView destroyPlayer];
  82. _playerView = nil;
  83. [[AppDelegate sharedAppDelegate].navigationViewController popViewControllerAnimated:nil];
  84. }
  85. - (void)viewWillAppear:(BOOL)animated {
  86. [super viewWillAppear:animated];
  87. self.navigationController.navigationBarHidden = YES;
  88. }
  89. /*
  90. #pragma mark - Navigation
  91. // In a storyboard-based application, you will often want to do a little preparation before navigation
  92. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  93. // Get the new view controller using [segue destinationViewController].
  94. // Pass the selected object to the new view controller.
  95. }
  96. */
  97. @end