GuideViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //
  2. // GuideViewController.m
  3. // yunbaolive
  4. //
  5. // Created by IOS1 on 2019/4/26.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "GuideViewController.h"
  9. #import "YBTabBarController.h"
  10. #import "AppDelegate.h"
  11. #import "PubH5.h"
  12. #import <AFNetworking/AFNetworking.h>
  13. #import <SDWebImage/SDWebImageDownloader.h>
  14. #import "YBLookVideoVC.h"
  15. @interface GuideViewController (){
  16. CAShapeLayer* _trackLayer;
  17. CAShapeLayer* _progressLayer;
  18. int curIndex;
  19. }
  20. @property (nonatomic,strong) NSArray *listArray;
  21. @property (nonatomic,strong) NSTimer *progressTimer;
  22. @property (nonatomic,assign) int showTime;
  23. @property (nonatomic,assign) int allTime;
  24. @property (nonatomic,assign) CGFloat countTime;
  25. @property (nonatomic,strong) UIButton *circleBtn;
  26. @property (nonatomic,strong) UIButton *jumpBtn;
  27. @property (nonatomic,strong) NSMutableArray *imgaeArray;
  28. @property (nonatomic,strong) UIImageView *launchImgaeV;
  29. @property (nonatomic,strong) NSURL *videoUrl;
  30. @property (nonatomic,strong) AVPlayer *videoPlayer;
  31. @property (nonatomic,strong) AVPlayerLayer *videoLayer;
  32. //监听播放起状态的监听者
  33. @property (nonatomic ,strong) id playerTimeObserver;
  34. @end
  35. @implementation GuideViewController
  36. -(void)viewWillAppear:(BOOL)animated{
  37. [super viewWillAppear:animated];
  38. [self requestData];
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. self.naviView.hidden = YES;
  43. self.navigationController.navigationBar.hidden = YES;
  44. _launchImgaeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  45. _launchImgaeV.image = [PublicObj getLaunchImage];
  46. _launchImgaeV.contentMode = UIViewContentModeScaleToFill;
  47. [self.view addSubview:_launchImgaeV];
  48. [YBNetworking getVideoClass:NO];
  49. //[self requestData];
  50. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goWeb)];
  51. [self.view addGestureRecognizer:tap];
  52. }
  53. - (void)requestData{
  54. //AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
  55. NSString *requestUrl = [purl stringByAppendingFormat:@"/?service=Guide.GetGuide"];
  56. [YBNetworking ybnetManager].requestSerializer.timeoutInterval = 5.0;
  57. [[YBNetworking ybnetManager] POST:requestUrl parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59. NSNumber *number = [responseObject valueForKey:@"ret"] ;
  60. if([number isEqualToNumber:[NSNumber numberWithInt:200]]) {
  61. NSDictionary *data = [responseObject valueForKey:@"data"];
  62. int code = [minstr([data valueForKey:@"code"]) intValue];
  63. if (![[data valueForKey:@"info"] isKindOfClass:[NSArray class]]) {
  64. [self jumpBtnClick];
  65. return;
  66. }
  67. NSArray *infoA = [NSArray arrayWithArray:[data valueForKey:@"info"]];
  68. if (code == 0) {
  69. curIndex = 0;
  70. NSDictionary *infoDIc = [infoA firstObject];
  71. if ([minstr([infoDIc valueForKey:@"switch"]) isEqual:@"1"]) {
  72. _listArray = [infoDIc valueForKey:@"list"];
  73. if ([minstr([infoDIc valueForKey:@"type"]) isEqual:@"0"]) {
  74. _showTime = [minstr([infoDIc valueForKey:@"time"]) intValue];
  75. _allTime = _showTime * (int)_listArray.count;
  76. [self showPic];
  77. }else{
  78. if (_listArray.count > 0) {
  79. _videoUrl = [NSURL URLWithString:minstr([_listArray[0] valueForKey:@"thumb"])];
  80. [self showVideo];
  81. }else{
  82. [self jumpBtnClick];
  83. }
  84. }
  85. }else{
  86. [self jumpBtnClick];
  87. }
  88. }else{
  89. [self jumpBtnClick];
  90. }
  91. }else{
  92. [self jumpBtnClick];
  93. }
  94. });
  95. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. [self jumpBtnClick];
  98. });
  99. }];
  100. }
  101. - (void)showPic{
  102. _imgaeArray = [NSMutableArray array];
  103. _countTime = 0.1;
  104. for (int i = 0; i < _listArray.count; i ++) {
  105. UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  106. imageV.contentMode = UIViewContentModeScaleAspectFill;
  107. imageV.clipsToBounds = YES;
  108. imageV.userInteractionEnabled = YES;
  109. [self.view addSubview:imageV];
  110. UILabel *tapsLb = [[UILabel alloc]init];
  111. tapsLb.frame = CGRectMake(_window_width *0.2, _window_height *0.85, _window_width*0.6, 46);
  112. tapsLb.backgroundColor = RGBA(1, 1, 1, 0.4);
  113. tapsLb.text = YZMsg(@"点击跳转第三方应用");
  114. tapsLb.font = [UIFont systemFontOfSize:16];
  115. tapsLb.textAlignment = NSTextAlignmentCenter;
  116. tapsLb.textColor = UIColor.whiteColor;
  117. tapsLb.layer.cornerRadius =23;
  118. tapsLb.layer.masksToBounds = YES;
  119. tapsLb.hidden = YES;
  120. [imageV addSubview:tapsLb];
  121. NSString *hrefStr = minstr([_listArray[i] valueForKey:@"href"]);
  122. if (hrefStr.length > 0 && ![PublicObj checkNull:hrefStr]) {
  123. tapsLb.hidden = NO;
  124. }else{
  125. tapsLb.hidden = YES;
  126. }
  127. if (i == 0) {
  128. imageV.hidden = NO;
  129. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:minstr([_listArray[i] valueForKey:@"thumb"])] options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. imageV.image = image;
  132. _launchImgaeV.hidden = YES;
  133. });
  134. }];
  135. }else{
  136. imageV.hidden = YES;
  137. [imageV sd_setImageWithURL:[NSURL URLWithString:minstr([_listArray[i] valueForKey:@"thumb"])]];
  138. }
  139. [_imgaeArray addObject:imageV];
  140. }
  141. _circleBtn = [UIButton buttonWithType:0];
  142. _circleBtn.frame = CGRectMake(_window_width-50, 40+statusbarHeight, 40, 40);
  143. [_circleBtn setTitle:YZMsg(@"跳过") forState:0];
  144. _circleBtn.titleLabel.font = [UIFont systemFontOfSize:13];
  145. _circleBtn.layer.cornerRadius = 20;
  146. _circleBtn.layer.masksToBounds = YES;
  147. [_circleBtn addTarget:self action:@selector(jumpBtnClick) forControlEvents:UIControlEventTouchUpInside];
  148. [_circleBtn setBackgroundColor:RGB_COLOR(@"#000000", 0.5)];
  149. [self.view addSubview:_circleBtn];
  150. float centerX = _circleBtn.width/2.0;
  151. float centerY = _circleBtn.height/2.0;
  152. //半径
  153. float radius = (_circleBtn.width-3)/2.0;
  154. //创建贝塞尔路径
  155. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f*M_PI clockwise:YES];
  156. //添加背景圆环
  157. CAShapeLayer *backLayer = [CAShapeLayer layer];
  158. backLayer.frame = _circleBtn.bounds;
  159. backLayer.fillColor = [[UIColor clearColor] CGColor];
  160. backLayer.strokeColor = [UIColor whiteColor].CGColor;
  161. backLayer.lineWidth = 3;
  162. backLayer.path = [path CGPath];
  163. backLayer.strokeEnd = 1;
  164. [_circleBtn.layer addSublayer:backLayer];
  165. //创建进度layer
  166. _progressLayer = [CAShapeLayer layer];
  167. _progressLayer.frame = _circleBtn.bounds;
  168. _progressLayer.fillColor = [[UIColor clearColor] CGColor];
  169. //指定path的渲染颜色
  170. _progressLayer.strokeColor = [[UIColor blackColor] CGColor];
  171. _progressLayer.lineCap = kCALineCapRound;
  172. _progressLayer.lineWidth = 3;
  173. _progressLayer.path = [path CGPath];
  174. _progressLayer.strokeEnd = 0;
  175. //设置渐变颜色
  176. CAGradientLayer *gradientLayer = [CAGradientLayer layer];
  177. gradientLayer.frame = _circleBtn.bounds;
  178. [gradientLayer setColors:[NSArray arrayWithObjects:(id)[RGB_COLOR(@"#ff7200", 1) CGColor],(id)[RGB_COLOR(@"#ff7200", 1) CGColor], nil]];
  179. gradientLayer.startPoint = CGPointMake(1, 1);
  180. gradientLayer.endPoint = CGPointMake(0, 0);
  181. [gradientLayer setMask:_progressLayer]; //用progressLayer来截取渐变层
  182. [_circleBtn.layer addSublayer:gradientLayer];
  183. _progressTimer = [NSTimer scheduledTimerWithTimeInterval:_countTime target:self selector:@selector(progresTimeDaoJiShi) userInfo:nil repeats:YES];
  184. _launchImgaeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  185. _launchImgaeV.image = [PublicObj getLaunchImage];
  186. [self.view addSubview:_launchImgaeV];
  187. }
  188. - (void)showVideo{
  189. NSDictionary *header = @{@"Referer":h5url};
  190. NSDictionary *optiosDic = @{@"AVURLAssetHTTPHeaderFieldsKey" : header};
  191. AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:_videoUrl options:optiosDic];
  192. AVPlayerItem *videoItem = [AVPlayerItem playerItemWithAsset:videoAsset];
  193. AVPlayer *player = [AVPlayer playerWithPlayerItem:videoItem];
  194. player.volume = 1.0;
  195. self.videoPlayer = player;
  196. _videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
  197. _videoLayer.videoGravity = AVLayerVideoGravityResize;
  198. _videoLayer.position = CGPointMake(_window_width/2, _window_height/2);
  199. _videoLayer.bounds = self.view.bounds;
  200. //Layer只能添加到Layer上面
  201. [self.view.layer addSublayer:_videoLayer];
  202. AVPlayerItem *playerItem = self.videoPlayer.currentItem;
  203. // 给AVPlayer添加观察者 必须实现 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
  204. //监控状态属性(AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态)
  205. [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
  206. // __weak typeof(self)WeakSelf = self;
  207. YBWeakSelf;
  208. //播放进度观察者 //设置每0.1秒执行一次
  209. _playerTimeObserver = [self.videoPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  210. //进度 当前时间/总时间
  211. CGFloat progress = CMTimeGetSeconds(weakSelf.videoPlayer.currentItem.currentTime) / CMTimeGetSeconds(weakSelf.videoPlayer.currentItem.duration);
  212. if (progress > 0.0f) {
  213. [weakSelf creatJumpBtn];
  214. }
  215. }];
  216. [self.videoPlayer play];
  217. _launchImgaeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  218. _launchImgaeV.image = [PublicObj getLaunchImage];
  219. [self.view addSubview:_launchImgaeV];
  220. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
  221. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  222. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
  223. }
  224. - (void)progresTimeDaoJiShi{
  225. _countTime += 0.1;
  226. _progressLayer.strokeEnd = _countTime/_allTime;
  227. [_progressLayer removeAllAnimations];
  228. int aaaa = _countTime *10;
  229. if (aaaa % (_showTime * 10) == 0) {
  230. int index = aaaa / (_showTime * 10);
  231. if (index < _imgaeArray.count) {
  232. curIndex = index;
  233. UIImageView *imgaeV = _imgaeArray[index-1];
  234. UIImageView *nextImgaeV = _imgaeArray[index];
  235. imgaeV.hidden = YES;
  236. nextImgaeV.hidden = NO;
  237. }
  238. }
  239. if (_countTime >= _allTime) {
  240. [self jumpBtnClick];
  241. }
  242. }
  243. - (void)jumpBtnClick{
  244. [self stop];
  245. if ([[[YBBaseAppDelegate sharedAppDelegate]topViewController]isKindOfClass:[YBLookVideoVC class]]) {
  246. [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"yb_dsp_reset"];
  247. return;
  248. }
  249. UIApplication *app =[UIApplication sharedApplication];
  250. AppDelegate *app2 = (AppDelegate *)app.delegate;
  251. YBNavigationController *nav = [[YBNavigationController alloc]initWithRootViewController:[[YBTabBarController alloc]initWithAlert:YES]];
  252. // YBNavigationController *nav = [[YBNavigationController alloc]initWithRootViewController:[[RTabBarController alloc]initWithAlert:YES]];
  253. app2.window.rootViewController = nav;
  254. }
  255. - (void)stop{
  256. if (_progressTimer) {
  257. [_progressTimer invalidate];
  258. _progressTimer = nil;
  259. }else{
  260. [_videoPlayer pause];
  261. _videoLayer = nil;
  262. }
  263. }
  264. - (void)dealloc {
  265. [self.videoPlayer.currentItem removeObserver:self forKeyPath:@"status"];
  266. [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
  267. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
  268. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
  269. }
  270. - (void)playFinished:(NSNotification *)not{
  271. [self jumpBtnClick];
  272. }
  273. #pragma mark
  274. #pragma mark 进入后台
  275. - (void)appDidEnterBackground:(NSNotification*)note
  276. {
  277. NSArray *tracks = [self.videoPlayer.currentItem tracks];
  278. for (AVPlayerItemTrack *playerItemTrack in tracks) {
  279. if ([playerItemTrack.assetTrack hasMediaCharacteristic:AVMediaCharacteristicVisual]) {
  280. playerItemTrack.enabled = YES;
  281. }
  282. }
  283. self.videoLayer.player = nil;
  284. self.videoPlayer.volume = 0;
  285. [self.videoPlayer play];
  286. }
  287. #pragma mark
  288. #pragma mark 进入前台
  289. - (void)appWillEnterForeground:(NSNotification*)note
  290. {
  291. NSArray *tracks = [self.videoPlayer.currentItem tracks];
  292. for (AVPlayerItemTrack *playerItemTrack in tracks) {
  293. if ([playerItemTrack.assetTrack hasMediaCharacteristic:AVMediaCharacteristicVisual]) {
  294. playerItemTrack.enabled = YES;
  295. }
  296. }
  297. self.videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
  298. self.videoLayer.frame = self.view.bounds;
  299. self.videoLayer.videoGravity = AVLayerVideoGravityResize;
  300. [self.view.layer insertSublayer:_videoLayer atIndex:0];
  301. self.videoPlayer.volume = 1;
  302. [self.videoPlayer play];
  303. }
  304. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  305. if ([keyPath isEqualToString:@"status"]) {
  306. AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
  307. switch (status)
  308. {
  309. /* Indicates that the status of the player is not yet known because
  310. it has not tried to load new media resources for playback */
  311. case AVPlayerStatusUnknown:
  312. {
  313. }
  314. break;
  315. case AVPlayerStatusReadyToPlay:
  316. {
  317. // [self creatJumpBtn];
  318. }
  319. break;
  320. case AVPlayerStatusFailed:
  321. {
  322. [self jumpBtnClick];
  323. }
  324. break;
  325. }
  326. }
  327. }
  328. - (void)creatJumpBtn{
  329. if (!_jumpBtn) {
  330. _launchImgaeV.hidden = YES;
  331. _jumpBtn = [UIButton buttonWithType:0];
  332. _jumpBtn.frame = CGRectMake(_window_width-50, 40+statusbarHeight, 40, 25);
  333. [_jumpBtn setTitle:YZMsg(@"跳过") forState:0];
  334. _jumpBtn.titleLabel.font = [UIFont systemFontOfSize:13];
  335. _jumpBtn.layer.cornerRadius = 12.5;
  336. _jumpBtn.layer.masksToBounds = YES;
  337. _jumpBtn.layer.borderWidth = 1;
  338. _jumpBtn.layer.borderColor = [UIColor whiteColor].CGColor;
  339. [ _jumpBtn addTarget:self action:@selector(jumpBtnClick) forControlEvents:UIControlEventTouchUpInside];
  340. [ _jumpBtn setBackgroundColor:RGB_COLOR(@"#000000", 0.5)];
  341. [self.view addSubview: _jumpBtn];
  342. }
  343. }
  344. - (void)goWeb{
  345. [self stop];
  346. PubH5 *web = [[PubH5 alloc]init];
  347. web.isGuide = YES;
  348. NSString *urlStr = minstr([_listArray[curIndex] valueForKey:@"href"]);
  349. if ([PublicObj checkNull:urlStr]) {
  350. [self jumpBtnClick];
  351. return;
  352. }
  353. web.url = urlStr;
  354. [self.navigationController pushViewController:web animated:YES];
  355. }
  356. /*
  357. #pragma mark - Navigation
  358. // In a storyboard-based application, you will often want to do a little preparation before navigation
  359. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  360. // Get the new view controller using [segue destinationViewController].
  361. // Pass the selected object to the new view controller.
  362. }
  363. */
  364. @end