XjAVPlayerSDK.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // XjAVPlayerSDK.m
  3. // XJAVPlayer
  4. //
  5. // Created by xj_love on 2016/10/27.
  6. // Copyright © 2016年 Xander. All rights reserved.
  7. //
  8. #import "XjAVPlayerSDK.h"
  9. #import "XJAVPlyer.h"
  10. #import "XJGestureButton.h"
  11. #import "XJBottomMenu.h"
  12. #import "XJTopMenu.h"
  13. #import "UIView+SCYCategory.h"
  14. #import "UIDevice+XJDevice.h"
  15. #define WS(weakSelf) __unsafe_unretained __typeof(&*self)weakSelf = self;
  16. @interface XjAVPlayerSDK (){
  17. BOOL isStop;//是否关闭过播放器(关闭,不是暂停)
  18. }
  19. @property (nonatomic, strong)XJAVPlyer *xjPlayer;
  20. @property (nonatomic, strong)XJGestureButton *backView;
  21. @property (nonatomic, strong)XJTopMenu *topMenu;
  22. @property (nonatomic, strong)XJBottomMenu *bottomMenu;
  23. @property (nonatomic, assign)CGRect firstFrame;//初始化的视屏大小
  24. @property (nonatomic, strong)NSString *saveUrl;//保存url;
  25. @property (nonatomic, strong)NSString *saveTitle;//保存标题
  26. @property (nonatomic, strong) UIActivityIndicatorView *loadingView;//菊花图
  27. @end
  28. @implementation XjAVPlayerSDK
  29. - (instancetype)initWithFrame:(CGRect)frame{
  30. if (self = [super initWithFrame:frame]) {
  31. [UIDevice setOrientation:UIInterfaceOrientationPortrait];
  32. self.firstFrame = frame;
  33. [self addAllView];
  34. }
  35. return self;
  36. }
  37. - (void)addAllView{
  38. [self.backView addSubview:self.topMenu];
  39. [self.backView addSubview:self.bottomMenu];
  40. [self.backView addSubview:self.loadingView];
  41. [self.xjPlayer addSubview:self.backView];
  42. [self addSubview:self.xjPlayer];
  43. }
  44. #pragma mark - **************************** 外部接口 *************************************
  45. - (void)xjStopPlayer{
  46. [self.xjPlayer xjStop];
  47. }
  48. - (CGFloat)xjCurrentTime{
  49. return self.xjPlayer.xjCurrentTime;
  50. }
  51. - (CGFloat)xjTotalTime{
  52. return self.xjPlayer.xjTotalTime;
  53. }
  54. - (void)xjPausePlayer{
  55. [self.xjPlayer xjPause];
  56. }
  57. - (void)xjPlayPlayer{
  58. [self.xjPlayer xjPlay];
  59. }
  60. #pragma mark - **************************** xjAVPlayer方法 ************************
  61. - (void)xjAVPlayerBLock{
  62. WS(weakSelf);
  63. //加载成功回调
  64. self.xjPlayer.xjPlaySuccessBlock = ^{
  65. // weakSelf.bottomMenu.xjPlay = YES;//如果想一进来就播放,就放开注释
  66. [weakSelf.loadingView stopAnimating];
  67. [weakSelf.loadingView setHidesWhenStopped:YES];
  68. };
  69. //播放失败回调
  70. self.xjPlayer.xjPlayFailBlock = ^{
  71. weakSelf->isStop = YES;//保证点击播放按钮能播放
  72. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  73. if (weakSelf.loadingView) {
  74. [weakSelf.loadingView stopAnimating];
  75. [weakSelf.loadingView setHidesWhenStopped:YES];
  76. }
  77. });
  78. };
  79. //加载进度
  80. self.xjPlayer.xjLoadedTimeBlock = ^(CGFloat time){
  81. weakSelf.bottomMenu.xjLoadedTimeRanges = time;
  82. };
  83. //视屏总长
  84. self.xjPlayer.xjTotalTimeBlock = ^(CGFloat time){
  85. weakSelf.bottomMenu.xjTotalTime = time;
  86. };
  87. //当前时间
  88. self.xjPlayer.xjCurrentTimeBlock = ^(CGFloat time){
  89. weakSelf.bottomMenu.xjCurrentTime = time;
  90. };
  91. //播放完
  92. self.xjPlayer.xjPlayEndBlock = ^{
  93. weakSelf.bottomMenu.xjPlayEnd = YES;
  94. //下一个
  95. if ([weakSelf.XjAVPlayerSDKDelegate respondsToSelector:@selector(xjNextPlayer)]) {
  96. [weakSelf.XjAVPlayerSDKDelegate xjNextPlayer];
  97. }
  98. };
  99. //关闭控件
  100. self.xjPlayer.xjPlayerStop = ^{
  101. weakSelf->isStop = YES;
  102. weakSelf.bottomMenu.xjPlayEnd = YES;
  103. };
  104. //方向改变
  105. self.xjPlayer.xjDirectionChange = ^(UIDeviceOrientation orient){
  106. if (weakSelf.xjAutoOrient) {
  107. if (orient == UIDeviceOrientationPortrait) {
  108. weakSelf.frame = weakSelf.firstFrame;
  109. weakSelf.bottomMenu.xjFull = NO;
  110. }else if(orient == UIDeviceOrientationLandscapeLeft||orient == UIDeviceOrientationLandscapeRight){
  111. weakSelf.frame = weakSelf.window.bounds;
  112. weakSelf.bottomMenu.xjFull = YES;
  113. }
  114. }
  115. };
  116. //播放延迟
  117. self.xjPlayer.xjDelayPlay = ^(BOOL flag){
  118. if (flag&&!weakSelf->isStop) {
  119. [weakSelf.loadingView startAnimating];
  120. }else{
  121. [weakSelf.loadingView stopAnimating];
  122. [weakSelf.loadingView setHidesWhenStopped:YES];
  123. }
  124. };
  125. }
  126. #pragma mark - **************************** XJGestureButton方法 **********************
  127. - (void)xjGestureButtonBlock{
  128. WS(weakSelf);
  129. //单击/双击事件
  130. self.backView.userTapGestureBlock = ^(NSUInteger number,BOOL flag){
  131. if (number == 1) {
  132. [UIView animateWithDuration:0.3 animations:^{
  133. weakSelf.topMenu.hidden = flag;
  134. weakSelf.bottomMenu.hidden = flag;
  135. }];
  136. }else if (number == 2){
  137. weakSelf.bottomMenu.xjPlay = flag;//不受flag影响
  138. }
  139. };
  140. //开始触摸
  141. self.backView.touchesBeganWithPointBlock = ^CGFloat(){
  142. //返回当前播放进度
  143. return [weakSelf.xjPlayer xjCurrentRate];
  144. };
  145. //结束触摸
  146. self.backView.touchesEndWithPointBlock = ^(CGFloat rate){
  147. //进度
  148. [weakSelf.xjPlayer xjSeekToTimeWithSeconds:[weakSelf.xjPlayer xjTotalTime]*rate];
  149. };
  150. }
  151. #pragma mark - **************************** XJTopMenu方法 *****************************
  152. - (void)xjTopMenuBlock{
  153. WS(weakSelf);
  154. //返回
  155. self.topMenu.xjTopGoBack = ^{
  156. if (weakSelf.bottomMenu.xjFull) {
  157. [UIDevice setOrientation:UIInterfaceOrientationPortrait];
  158. weakSelf.frame = weakSelf.firstFrame;
  159. weakSelf.bottomMenu.xjFull = NO;
  160. }else{
  161. if ([weakSelf.XjAVPlayerSDKDelegate respondsToSelector:@selector(xjGoBack)]) {
  162. [weakSelf.XjAVPlayerSDKDelegate xjGoBack];
  163. }
  164. }
  165. };
  166. }
  167. #pragma mark - **************************** XJBottomMenu方法 **************************
  168. - (void)xjBottomMenuBlock{
  169. WS(weakSelf);
  170. //播放/暂停
  171. self.bottomMenu.xjPlayOrPauseBlock = ^(BOOL isPlay){
  172. if (weakSelf->isStop) {
  173. weakSelf->isStop = NO;
  174. weakSelf.xjPlayer.xjPlayerUrl = weakSelf.saveUrl;
  175. weakSelf.topMenu.xjAvTitle = weakSelf.saveTitle;
  176. }
  177. if (isPlay) {
  178. [weakSelf.xjPlayer xjPlay];
  179. }else{
  180. [weakSelf.xjPlayer xjPause];
  181. }
  182. };
  183. //下一个
  184. self.bottomMenu.xjNextPlayerBlock = ^{
  185. weakSelf.bottomMenu.xjPlayEnd = YES;
  186. if ([weakSelf.XjAVPlayerSDKDelegate respondsToSelector:@selector(xjNextPlayer)]) {
  187. [weakSelf.XjAVPlayerSDKDelegate xjNextPlayer];
  188. }
  189. };
  190. //滑动条滑动时
  191. self.bottomMenu.xjSliderValueChangeBlock = ^(CGFloat time){
  192. [weakSelf.xjPlayer xjSeekToTimeWithSeconds:time];
  193. [weakSelf.xjPlayer xjPause];
  194. };
  195. //滑动条拖动完成
  196. self.bottomMenu.xjSliderValueChangeEndBlock = ^(CGFloat time){
  197. [weakSelf.xjPlayer xjSeekToTimeWithSeconds:time];
  198. };
  199. //放大/缩小
  200. self.bottomMenu.xjFullOrSmallBlock = ^(BOOL isFull){
  201. if (isFull) {
  202. [UIDevice setOrientation:UIInterfaceOrientationLandscapeRight];
  203. weakSelf.frame = weakSelf.window.bounds;
  204. [weakSelf prefersStatusBarHidden];
  205. }else{
  206. [UIDevice setOrientation:UIInterfaceOrientationPortrait];
  207. weakSelf.frame = weakSelf.firstFrame;
  208. }
  209. };
  210. }
  211. - (BOOL)prefersStatusBarHidden{
  212. return YES;
  213. }
  214. #pragma mark - **************************** 懒加载 ****************************************
  215. - (XJAVPlyer *)xjPlayer{
  216. if (_xjPlayer == nil) {
  217. _xjPlayer = [[XJAVPlyer alloc] init];
  218. [self xjAVPlayerBLock];
  219. }
  220. return _xjPlayer;
  221. }
  222. - (void)setXjPlayerUrl:(NSString *)xjPlayerUrl{
  223. _xjPlayerUrl = xjPlayerUrl;
  224. self.saveUrl = _xjPlayerUrl;
  225. self.xjPlayer.xjPlayerUrl = _xjPlayerUrl;
  226. }
  227. - (void)setXjLastTime:(int)xjLastTime{
  228. _xjLastTime = xjLastTime;
  229. [self.xjPlayer xjSeekToTimeWithSeconds:_xjLastTime];
  230. }
  231. - (XJGestureButton *)backView{
  232. if (_backView == nil) {
  233. _backView = [[XJGestureButton alloc] init];
  234. [self xjGestureButtonBlock];
  235. }
  236. return _backView;
  237. }
  238. - (XJTopMenu *)topMenu{
  239. if (_topMenu == nil) {
  240. _topMenu = [[XJTopMenu alloc] init];
  241. _topMenu.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0];
  242. _topMenu.hidden = YES;
  243. [self xjTopMenuBlock];
  244. }
  245. return _topMenu;
  246. }
  247. - (void)setXjPlayerTitle:(NSString *)xjPlayerTitle{
  248. _xjPlayerTitle = xjPlayerTitle;
  249. self.saveTitle = _xjPlayerTitle;
  250. self.topMenu.xjAvTitle = self.xjPlayerTitle;
  251. }
  252. - (XJBottomMenu *)bottomMenu{
  253. if (_bottomMenu == nil) {
  254. _bottomMenu = [[XJBottomMenu alloc] init];
  255. _bottomMenu.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0];
  256. _bottomMenu.hidden = YES;
  257. [self xjBottomMenuBlock];
  258. }
  259. return _bottomMenu;
  260. }
  261. - (UIActivityIndicatorView *)loadingView{
  262. if (_loadingView == nil) {
  263. _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  264. [_loadingView startAnimating];
  265. }
  266. return _loadingView;
  267. }
  268. #pragma mark - **************************** 布局 *************************************
  269. - (void)layoutSubviews{
  270. [super layoutSubviews];
  271. self.xjPlayer.frame = CGRectMake(0, 0, self.width, self.height);
  272. self.backView.frame = self.xjPlayer.frame;
  273. self.topMenu.frame = CGRectMake(0, self.backView.top, self.backView.width, 40);
  274. self.bottomMenu.frame = CGRectMake(0, self.backView.height-40, self.backView.width, 40);
  275. self.loadingView.center = CGPointMake(self.backView.centerX, self.backView.centerY);
  276. }
  277. @end