LeeVideoVC.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // LeeVideoVC.m
  3. // jiaPei
  4. //
  5. // Created by EchoShacolee on 2018/3/8.
  6. // Copyright © 2018年 JCZ. All rights reserved.
  7. //
  8. #import "LeeVideoVC.h"
  9. #import "AGPlayerView.h"
  10. #import <RealReachability/RealReachability.h>
  11. @interface LeeVideoVC ()
  12. {
  13. BOOL isGoOn4G;
  14. NSString *urlStr;
  15. }
  16. @property (strong, nonatomic) AGPlayerView *playerView;
  17. @end
  18. @implementation LeeVideoVC
  19. -(void)dealloc{
  20. [GLobalRealReachability stopNotifier];
  21. [[NSNotificationCenter defaultCenter]removeObserver:self name:kRealReachabilityChangedNotification object:nil];
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"videoFinish%@%@",_dataDic[@"id"],[self getVideoFormat]]];
  26. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  27. //本地视频
  28. NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"video%@%@",_dataDic[@"id"],[self getVideoFormat]]];
  29. urlStr = videoPath;
  30. NSURL *url = [NSURL fileURLWithPath:urlStr isDirectory:NO];
  31. [self.playerView updatePlayerWithURL:url];
  32. }else{
  33. //网络监测
  34. [GLobalRealReachability startNotifier];
  35. ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
  36. if (status == RealStatusNotReachable) {
  37. ShowMsg(@"当前无网络连接");
  38. } else if (status == RealStatusViaWWAN){
  39. // ShowMsg(@"你正在使用的是运营商网络,继续观看可能会产生超额流量费用");
  40. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该视频还未下载,在线播放会产生大量流量,建议在WiFi环境下下载至本地后播放" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"下载视频",@"继续播放", nil];
  41. alert.tag = 1;
  42. [alert show];
  43. }else{
  44. //真Wi-Fi 播放吧 //未知 不知道是有网还是没网 索性播放了吧
  45. urlStr = _dataDic[@"videoPath"];
  46. NSURL *url = [NSURL URLWithString:urlStr];
  47. [self.playerView updatePlayerWithURL:url];
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(networkChanged:)
  50. name:kRealReachabilityChangedNotification
  51. object:nil];
  52. }
  53. }
  54. [self myInit];
  55. }
  56. -(AGPlayerView *)playerView{
  57. if (!_playerView) {
  58. CGFloat zz = 0;
  59. if (is_iPhoneX) {
  60. zz = kStatusHeight;
  61. }
  62. _playerView = [[AGPlayerView alloc]initWithFrame:CGRectMake(0, zz, kSize.width, kSize.width*14/25)];
  63. [self.view addSubview:_playerView];
  64. }
  65. return _playerView;
  66. }
  67. -(void)viewWillDisappear:(BOOL)animated{
  68. [super viewWillDisappear:animated];
  69. if (self.playerView.isPlaying) {
  70. [self.playerView pause];
  71. }
  72. [self.playerView removeObserveAndNOtification];
  73. [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
  74. [[UIApplication sharedApplication] setStatusBarStyle:(UIStatusBarStyleDefault)];
  75. }
  76. -(void)myInit{
  77. self.view.backgroundColor = [UIColor blackColor];
  78. self.playerView.titleLabel.text = _dataDic[@"title"];
  79. __weak typeof(self)weakSelf = self;
  80. self.playerView.goBackBlock = ^{
  81. if (weakSelf.playerView.isLandscape) {
  82. weakSelf.playerView.isLandscape = NO;
  83. return;
  84. }
  85. if (weakSelf.playerView.isPlaying) {
  86. [weakSelf.playerView pause];
  87. }
  88. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  89. };
  90. CGFloat y = CGRectGetMaxY(self.playerView.frame);
  91. CGFloat h = kSize.height - y;
  92. UIView* titleBar = [[UIView alloc] initWithFrame:CGRectMake(0, y, kSize.width, h)];
  93. titleBar.backgroundColor = [UIColor whiteColor];
  94. [self.view addSubview:titleBar];
  95. [self.view bringSubviewToFront:self.playerView];
  96. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, kSize.width, 20)];
  97. [label setText:@"内容详解" Font:FontTitle TextColor:titleColor Alignment:NSTextAlignmentLeft];
  98. [titleBar addSubview:label];
  99. UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 40, kSize.width - 40, titleBar.height - 50)];
  100. textView.font = [UIFont scaleSize:Font17];
  101. textView.textColor = contentTextColor;
  102. textView.text = _dataDic[@"content"];
  103. if ([textView.text length] == 0) {
  104. textView.text = @"暂无详解";
  105. }
  106. textView.editable = NO;
  107. [titleBar addSubview:textView];
  108. }
  109. #pragma mark - 网络监测
  110. - (NSInteger)networkChanged:(NSNotification *)notification
  111. {
  112. RealReachability *reachability = (RealReachability *)notification.object;
  113. ReachabilityStatus status = [reachability currentReachabilityStatus];
  114. //如果变成没网 暂停 如果Wi-Fi突然换到流量 暂停视频 询问是否继续。其他不作处理
  115. if (_playerView.isPlaying) {
  116. if (status == RealStatusNotReachable) {
  117. ShowMsg(@"当前无网络连接");
  118. [self.playerView pause];
  119. }else if (status == RealStatusViaWWAN){
  120. if (!isGoOn4G) {
  121. [_playerView pause];
  122. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的Wi-Fi已断开连接,继续播放将产生数据流量,是否继续播放?" delegate:self cancelButtonTitle:@"停止播放" otherButtonTitles:@"继续播放", nil];
  123. alert.tag = 2;
  124. [alert show];
  125. }
  126. }else{
  127. //wifi/未知情况
  128. }
  129. }
  130. return status;
  131. }
  132. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  133. if (alertView.tag == 1) {
  134. if (buttonIndex == alertView.cancelButtonIndex) {
  135. [self dismissViewControllerAnimated:YES completion:nil];
  136. }else if (buttonIndex == 1){
  137. if (self.goDownLoadBlock) {
  138. self.goDownLoadBlock();
  139. }
  140. [self dismissViewControllerAnimated:YES completion:nil];
  141. }else{
  142. [self.playerView updatePlayerWithURL:[NSURL URLWithString:_dataDic[@"videoPath"]]];
  143. isGoOn4G = YES;
  144. }
  145. }else if (alertView.tag == 2){
  146. if (buttonIndex == alertView.cancelButtonIndex) {
  147. //停止播放
  148. if (self.playerView.isPlaying) {
  149. [self.playerView pause];
  150. }
  151. }else{
  152. [self.playerView play];
  153. isGoOn4G = YES;
  154. }
  155. }
  156. }
  157. - (NSString *)getVideoFormat{
  158. NSString *format = [NSString stringWithFormat:@".%@",[[self.dataDic[@"videoPath"] componentsSeparatedByString:@"."] lastObject]];
  159. return format;
  160. }
  161. - (void)didReceiveMemoryWarning {
  162. [super didReceiveMemoryWarning];
  163. // Dispose of any resources that can be recreated.
  164. }
  165. @end