sliderCollectionView.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // sliderCollectionView.m
  3. // yunbaolive
  4. //
  5. // Created by IOS1 on 2019/3/23.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "sliderCollectionView.h"
  9. #import "sliderCollectionVCell.h"
  10. #import <ZFPlayer/ZFPlayer.h>
  11. #import <ZFPlayer/ZFAVPlayerManager.h>
  12. #import <ZFPlayer/ZFIJKPlayerManager.h>
  13. #import <ZFPlayer/ZFPlayerControlView.h>
  14. #import "YBGoodPlayerCtrView.h"
  15. @interface sliderCollectionView()
  16. @property (nonatomic, strong) ZFPlayerController *player;
  17. @property(nonatomic,strong)YBGoodPlayerCtrView *controlView;
  18. @end
  19. @implementation sliderCollectionView{
  20. UICollectionView *collectionV;
  21. NSArray *listArray;
  22. BOOL isVideo;
  23. UIImageView *videoBackImg;
  24. UIButton *pauseBtn;
  25. UIButton *muteBtn;
  26. BOOL isPause;
  27. NSString *_videoUrl;
  28. }
  29. -(instancetype)initWithFrame:(CGRect)frame{
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. isPause = NO;
  33. [self creatUI];
  34. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopGoodsVideo) name:@"stopGoodsVideo" object:nil];
  35. }
  36. return self;
  37. }
  38. -(void)creatUI{
  39. _backScroll = [[UIScrollView alloc]init];
  40. _backScroll.frame = CGRectMake(0, 0, _window_width, self.height);
  41. _backScroll.pagingEnabled = YES;
  42. _backScroll.delegate = self;
  43. [self addSubview:_backScroll];
  44. countLb = [[UILabel alloc]init];
  45. countLb.frame = CGRectMake(_window_width-55, self.height-55, 40, 22);
  46. countLb.backgroundColor = RGBA(5, 3, 3, 0.4);
  47. countLb.font = [UIFont systemFontOfSize:12];
  48. countLb.textColor = [UIColor whiteColor];
  49. countLb.textAlignment = NSTextAlignmentCenter;
  50. countLb.layer.cornerRadius = 11;
  51. countLb.layer.masksToBounds = YES;
  52. countLb.hidden = YES;
  53. [self addSubview:countLb];
  54. }
  55. - (void)reload:(NSArray *)array andIsvideo:(BOOL)video{
  56. listArray = array;
  57. isVideo = video;
  58. _backScroll.contentSize = CGSizeMake(_window_width *listArray.count, self.height);
  59. for (int i = 0; i < listArray.count; i ++) {
  60. UIImageView *img = [[UIImageView alloc]init];
  61. img.frame = CGRectMake(i * _window_width, 0, _window_width, self.height);
  62. img.contentMode = UIViewContentModeScaleAspectFill;
  63. img.clipsToBounds = YES;
  64. img.backgroundColor = [UIColor blackColor];
  65. [_backScroll addSubview:img];
  66. if (isVideo) {
  67. if (i == 0) {
  68. videoBackImg = img;
  69. _videoUrl = minstr(listArray[i]);
  70. [self initPlayer];
  71. [_controlView ctrVideoPlay:YES];
  72. //_controlView会打断_backScroll的滚动,这里利用遮罩避免问题
  73. UIButton *tapbtn = [UIButton buttonWithType:0];
  74. tapbtn.frame =CGRectMake(0, 0, videoBackImg.width, videoBackImg.height);
  75. [tapbtn addTarget:self action:@selector(pauseBtnClick) forControlEvents:UIControlEventTouchUpInside];
  76. [videoBackImg addSubview:tapbtn];
  77. muteBtn = [UIButton buttonWithType:0];
  78. muteBtn.frame = CGRectMake(_window_width-55, self.height-55-45, 36, 36);
  79. [muteBtn setImage:[UIImage imageNamed:@"mute_normal"] forState:UIControlStateNormal];
  80. [muteBtn setImage:[UIImage imageNamed:@"mute_sel"] forState:UIControlStateSelected];
  81. [muteBtn addTarget:self action:@selector(muteBtnClick) forControlEvents:UIControlEventTouchUpInside];
  82. [videoBackImg addSubview:muteBtn];
  83. muteBtn.hidden = NO;
  84. muteBtn.selected = NO;
  85. }else{
  86. [img sd_setImageWithURL:[NSURL URLWithString:minstr(listArray[i])]];
  87. }
  88. }else{
  89. [img sd_setImageWithURL:[NSURL URLWithString:minstr(listArray[i])]];
  90. }
  91. }
  92. if (listArray.count > 1) {
  93. countLb.hidden = NO;
  94. }
  95. countLb.text = [NSString stringWithFormat:@"1/%ld", listArray.count];
  96. }
  97. //点击暂停
  98. -(void)pauseBtnClick{
  99. isPause = !isPause;
  100. if (isPause) {
  101. [_controlView ctrVideoPlay:NO];
  102. muteBtn.hidden = YES;
  103. }else{
  104. [_controlView ctrVideoPlay:YES];
  105. muteBtn.hidden = NO;
  106. }
  107. }
  108. -(void)muteBtnClick{
  109. muteBtn.selected = !muteBtn.selected;
  110. if (muteBtn.selected) {
  111. [self.player.currentPlayerManager setMuted:YES];
  112. }else{
  113. [self.player.currentPlayerManager setMuted:NO];
  114. }
  115. }
  116. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  117. _page = scrollView.contentOffset.x/_window_width;
  118. if (isVideo) {
  119. if (_page == 0) {
  120. [self resumeGoodsVideo];
  121. }else{
  122. isPause = YES;
  123. if (isPause) {
  124. [_controlView ctrVideoPlay:NO];
  125. }
  126. }
  127. }
  128. countLb.text = [NSString stringWithFormat:@"%d/%ld",_page+1, listArray.count];
  129. }
  130. -(void)stopGoodsVideo{
  131. isPause = YES;
  132. if (isPause) {
  133. [_controlView ctrVideoPlay:NO];
  134. }
  135. }
  136. -(void)resumeGoodsVideo{
  137. if (isVideo) {
  138. isPause = NO;
  139. [_controlView ctrVideoPlay:YES];
  140. muteBtn.hidden = NO;
  141. }
  142. }
  143. -(void)initPlayer {
  144. if (self.player) {
  145. return;
  146. }
  147. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  148. // playerManager.requestHeader = @{@"Referer":h5url};
  149. NSDictionary *header = @{@"Referer":h5url};
  150. NSDictionary *optiosDic = @{@"AVURLAssetHTTPHeaderFieldsKey" : header};
  151. [playerManager setRequestHeader:optiosDic];
  152. /// player的tag值必须在cell里设置
  153. self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:videoBackImg];
  154. self.player.controlView = self.controlView;
  155. //[self.player setDisableGestureTypes:ZFPlayerDisableGestureTypesAll];
  156. self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFill;
  157. /// 竖屏的全屏
  158. self.player.orientationObserver.fullScreenMode = ZFFullScreenModePortrait;
  159. /*
  160. /// 隐藏全屏的状态栏
  161. self.player.orientationObserver.fullScreenStatusBarHidden = YES;
  162. self.player.orientationObserver.fullScreenStatusBarAnimation = UIStatusBarAnimationNone;
  163. /// 全屏的填充模式(全屏填充、按视频大小填充)
  164. self.player.orientationObserver.portraitFullScreenMode = ZFPortraitFullScreenModeScaleAspectFit;
  165. /// 禁用竖屏全屏的手势(点击、拖动手势)
  166. self.player.orientationObserver.disablePortraitGestureTypes = ZFDisablePortraitGestureTypesNone;
  167. */
  168. self.player.playerDisapperaPercent = 1.0;
  169. NSURL *pathUrl = [NSURL URLWithString:_videoUrl];
  170. self.player.assetURL = pathUrl;
  171. //功能
  172. self.player.playerPrepareToPlay = ^(id<ZFPlayerMediaPlayback> _Nonnull asset, NSURL * _Nonnull assetURL) {
  173. NSLog(@"准备");
  174. };
  175. YBWeakSelf;
  176. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  177. NSLog(@"结束");
  178. [weakSelf.player.currentPlayerManager replay];
  179. };
  180. /*
  181. /// 封面信息
  182. [self.controlView showCoverViewWithUrl:_itemLayout.data.video_thumb];
  183. CGSize videoSize = CGSizeMake(_itemLayout.data.width, _itemLayout.data.height);
  184. self.player.currentPlayerManager.presentationSize = videoSize;
  185. */
  186. }
  187. - (YBGoodPlayerCtrView *)controlView {
  188. if (!_controlView) {
  189. _controlView = [YBGoodPlayerCtrView new];
  190. YBWeakSelf;
  191. _controlView.ctrEvent = ^{
  192. [weakSelf pauseBtnClick];
  193. };
  194. }
  195. return _controlView;
  196. }
  197. @end