SimulationExamRecordController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // SimulationExamRecordController.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/9/21.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "SimulationExamRecordController.h"
  9. #import "RecordBodyView.h"
  10. #import "RecordListCell.h"
  11. #import "RecordBottomView.h"
  12. #import "RecordTipsView.h"
  13. #import "WMPlayer.h"
  14. #import "SongModel.h"
  15. #import "KSGuideMaskView.h"
  16. #import "KSNormalAlertView.h"
  17. #import "KSVideoRecordViewController.h"
  18. #import "TZImageManager.h"
  19. @interface SimulationExamRecordController ()<UITableViewDelegate, UITableViewDataSource,WMPlayerDelegate>
  20. {
  21. WMPlayer *_wmPlayer;
  22. CGRect _playerFrame;
  23. }
  24. @property (nonatomic, assign) BOOL isRatation;
  25. @property (nonatomic, strong) UIView *bgView;
  26. @property (nonatomic, strong) RecordBodyView *topView;
  27. @property (nonatomic, strong) RecordTipsView *tipsView;
  28. @property (nonatomic, strong) UITableView *tableView;
  29. @property (nonatomic, strong) RecordBottomView *bottomView;
  30. @property (nonatomic, strong) NSMutableArray *songArray;
  31. @property (strong, nonatomic) MBProgressHUD *HUD;
  32. @property (nonatomic, strong) NSMutableArray *fileUrlArray;
  33. @property (nonatomic, assign) BOOL hasShowTips;
  34. @end
  35. @implementation SimulationExamRecordController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view.
  39. self.ks_prefersNavigationBarHidden = YES;
  40. [self configUI];
  41. [self evaluateSource];
  42. }
  43. - (void)viewWillAppear:(BOOL)animated {
  44. [super viewWillAppear:animated];
  45. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  46. }
  47. - (void)viewDidDisappear:(BOOL)animated {
  48. [super viewDidDisappear:animated];
  49. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  50. if(@available(iOS 13.0, *)){
  51. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent;
  52. }
  53. }
  54. - (void)evaluateSource {
  55. if (self.sourceModel == nil) {
  56. return;
  57. }
  58. [self.topView configTime:[NSString stringWithFormat:@"%.0f", self.sourceModel.subTime]];
  59. [self.tipsView configWithEndTime:self.sourceModel.examEndTime];
  60. NSData *songDate = [self.sourceModel.songJson dataUsingEncoding:NSUTF8StringEncoding];
  61. NSArray *listArray = [NSJSONSerialization JSONObjectWithData:songDate options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
  62. NSMutableArray *songList = [NSMutableArray array];
  63. for (NSDictionary *parm in listArray) {
  64. SongModel *model = [[SongModel alloc] initWithDictionary:parm];
  65. [songList addObject:model];
  66. }
  67. self.songArray = [NSMutableArray arrayWithArray:songList];
  68. for (NSInteger i = 0; i < self.songArray.count; i++) {
  69. SongModel *model = self.songArray[i];
  70. NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName];
  71. NSDictionary *fileMessage = UserDefault(fileKey);
  72. if (fileMessage) {
  73. NSString *remoteUrl = [fileMessage stringValueForKey:@"remoteUrl"];
  74. [self.fileUrlArray addObject:remoteUrl];
  75. }
  76. }
  77. [self checkSubmitButtonEnable];
  78. [self.tableView reloadData];
  79. }
  80. - (void)configUI {
  81. [self.view addSubview:self.topView];
  82. [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.left.right.mas_equalTo(self.view);
  84. make.height.mas_equalTo(self.view.mas_width).multipliedBy(79.0 / 207).offset(11);
  85. }];
  86. [self.view addSubview:self.bottomView];
  87. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.left.right.mas_equalTo(self.view);
  89. make.bottom.mas_equalTo(self.view.mas_bottomMargin);
  90. make.height.mas_equalTo(90);
  91. }];
  92. [self.view addSubview:self.tableView];
  93. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.right.mas_equalTo(self.view);
  95. make.top.mas_equalTo(self.topView.mas_bottom);
  96. make.bottom.mas_equalTo(self.bottomView.mas_top);
  97. }];
  98. }
  99. - (void)viewDidAppear:(BOOL)animated {
  100. [super viewDidAppear:animated];
  101. if (_hasShowTips == NO) {
  102. [self addIntroduceView];
  103. }
  104. }
  105. - (void)addIntroduceView {
  106. _hasShowTips = YES;
  107. CGRect rect1 = [self.tableView convertRect:self.tableView.tableHeaderView.frame toView:[UIApplication sharedApplication].keyWindow];
  108. rect1.origin.y -= 50;
  109. rect1.size.height += 50;
  110. UIBezierPath *pathOne = [UIBezierPath bezierPathWithRect:rect1];
  111. CGRect rect2 = rect1;
  112. rect2.size = CGSizeMake(kScreenWidth, 130);
  113. rect2.origin.y = CGRectGetMaxY(rect1);
  114. UIBezierPath *pathSecond = [UIBezierPath bezierPathWithRect:rect2];
  115. NSArray *tipsArray = @[@"请在录播时间内完成视频上传", @"点击上传曲目录播视频"];
  116. NSArray *bezierArray = @[pathOne,pathSecond];
  117. KSGuideMaskView *guideView = [[KSGuideMaskView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
  118. [guideView addTips:tipsArray transparentRect:bezierArray shaperLayerIndex:-1];
  119. [guideView showMaskViewInView:nil];
  120. }
  121. #pragma mark ----- table data source
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  123. return self.songArray.count;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  126. SongModel *model = self.songArray[indexPath.row];
  127. RecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecordListCell"];
  128. MJWeakSelf;
  129. [cell configCellWithSource:model examRegistrationId:self.examRegistrationId operationCallback:^(RECORDTYPE type, NSString *fileKey) {
  130. [weakSelf opreationCellType:type fileKey:fileKey index:indexPath.row];
  131. }];
  132. return cell;
  133. }
  134. - (void)opreationCellType:(RECORDTYPE)type fileKey:(NSString *)fileKey index:(NSInteger)cellIndex {
  135. if (type == RECORDTYPE_RECORD) { // 录像
  136. NSMutableArray *songNameArray = [NSMutableArray array];
  137. for (NSInteger i = 0; i < self.songArray.count; i++) {
  138. if (i >= cellIndex) {
  139. // 从选择的开始->后面未录制的添加到待录制里
  140. SongModel *model = self.songArray[i];
  141. NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName];
  142. NSDictionary *fileMessage = UserDefault(fileKey);
  143. if (fileMessage == nil) {
  144. [songNameArray addObject:model.songName];
  145. }
  146. }
  147. }
  148. KSVideoRecordViewController *recordCtrl = [[KSVideoRecordViewController alloc] init];
  149. recordCtrl.subjectName = self.subjectName;
  150. recordCtrl.leverName = self.leverName;
  151. recordCtrl.songMessageArray = songNameArray;
  152. recordCtrl.singleSongRecordMinutes = self.sourceModel.singleSongRecordMinutes;
  153. [recordCtrl configTime:self.topView.duration recordFinishBlock:^(VIDEORECORDACTION action, NSMutableArray *videoArray) {
  154. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  155. [self refreshAllWithVideoArray:videoArray songArray:songNameArray];
  156. });
  157. }];
  158. recordCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
  159. [self presentViewController:recordCtrl animated:YES completion:nil];
  160. }
  161. else { // 删除
  162. [self deleteFileWithKey:fileKey];
  163. }
  164. }
  165. - (void)refreshAllWithVideoArray:(NSMutableArray *)videoArray songArray:(NSArray *)songArray {
  166. dispatch_main_async_safe(^{
  167. [MBProgressHUD ksShowHUDWithText:@"视频处理中..."];
  168. });
  169. [self outputVideoFileIndex:0 videoArray:videoArray songArray:songArray];
  170. }
  171. - (void)outputVideoFileIndex:(NSInteger)index videoArray:(NSArray *)videoArray songArray:(NSArray *)songArray {
  172. __block NSInteger fileIndex = index;
  173. if (videoArray.count && videoArray.count > index) {
  174. PHAsset *assset = videoArray[index];
  175. MJWeakSelf;
  176. [[TZImageManager manager] getVideoOutputPathWithAsset:assset presetName:AVAssetExportPresetMediumQuality success:^(NSString *outputPath) {
  177. NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
  178. NSData *outputData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:outputPath]]; //压缩后的视频
  179. NSLog(@"导出后的视频:%@",[NSString stringWithFormat:@"%.2fM",(CGFloat)outputData.length/(1024*1024)]);
  180. NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, songArray[fileIndex]];
  181. dispatch_main_async_safe(^{
  182. fileIndex++;
  183. [weakSelf refreshView:outputPath fileKey:fileKey];
  184. if (fileIndex == videoArray.count) {
  185. [MBProgressHUD ksHideHUD];
  186. }
  187. else {
  188. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  189. [weakSelf outputVideoFileIndex:fileIndex videoArray:videoArray songArray:songArray];
  190. });
  191. }
  192. });
  193. } failure:^(NSString *errorMessage, NSError *error) {
  194. dispatch_main_async_safe(^{
  195. [MBProgressHUD ksShowMessage:@"视频导出失败"];
  196. fileIndex++;
  197. if (fileIndex == videoArray.count) {
  198. [MBProgressHUD ksHideHUD];
  199. }
  200. else {
  201. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  202. [self outputVideoFileIndex:fileIndex videoArray:videoArray songArray:songArray];
  203. });
  204. }
  205. });
  206. }];
  207. }
  208. }
  209. - (void)refreshView:(NSString *)videoUrl fileKey:(NSString *)fileKey {
  210. NSString *fileUrl = videoUrl;
  211. NSString *fileName = [[videoUrl componentsSeparatedByString:@"/"] lastObject];
  212. // 保存文件路径
  213. NSDictionary *parm = @{@"localFileUrl":videoUrl,
  214. @"fileName": fileName};
  215. UserDefaultSet(parm, fileKey);
  216. [self.tableView reloadData];
  217. [self.fileUrlArray addObject:fileUrl];
  218. [self checkSubmitButtonEnable];
  219. }
  220. - (void)deleteFileWithKey:(NSString *)fileKey {
  221. NSDictionary *parm = UserDefault(fileKey);
  222. NSString *localUrl = [parm stringValueForKey:@"localFileUrl"];
  223. [self removeVideoWithPath:localUrl];
  224. UserDefaultRemoveObjectForKey(fileKey);
  225. [self.fileUrlArray removeObject:localUrl];
  226. [self checkSubmitButtonEnable];
  227. [self.tableView reloadData];
  228. }
  229. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  230. RecordListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  231. if (cell.fileMessage) {
  232. // 播放
  233. NSString *localUrl = [cell.fileMessage stringValueForKey:@"localFileUrl"];
  234. if ([[NSFileManager defaultManager] fileExistsAtPath:localUrl]) {
  235. [self playVideoWithUrl:[NSURL fileURLWithPath:localUrl]];
  236. }
  237. else {
  238. NSString *remoteUrl = [cell.fileMessage stringValueForKey:@"remoteUrl"];
  239. [self playVideoWithUrl:[NSURL URLWithString:remoteUrl]];
  240. }
  241. }
  242. }
  243. - (void)checkSubmitButtonEnable {
  244. if (self.fileUrlArray.count == 0) {
  245. self.bottomView.finishButton.userInteractionEnabled = NO;
  246. [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_unable"] forState:UIControlStateNormal];
  247. }
  248. else {
  249. self.bottomView.finishButton.userInteractionEnabled = YES;
  250. [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_nomal"] forState:UIControlStateNormal];
  251. [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_highlight"] forState:UIControlStateHighlighted];
  252. }
  253. }
  254. #pragma mark --- lazying
  255. - (RecordBodyView *)topView {
  256. if (!_topView) {
  257. _topView = [RecordBodyView shareInstance];
  258. MJWeakSelf;
  259. [_topView topviewAction:^(RECORDTOPACTION action) {
  260. [weakSelf backOrEndAction:action];
  261. }];
  262. }
  263. return _topView;
  264. }
  265. - (void)backOrEndAction:(RECORDTOPACTION)action {
  266. if (action == RECORDTOPACTION_BACK) {
  267. [self.navigationController popViewControllerAnimated:YES];
  268. }
  269. else { // 时间结束
  270. MJWeakSelf;
  271. if (self.fileUrlArray.count == 0) {
  272. [self KSShowMsg:@"考试结束" promptCompletion:^{
  273. [weakSelf.navigationController popViewControllerAnimated:YES];
  274. }];
  275. }
  276. else {
  277. [KSNormalAlertView ks_showAlertWithTitle:@"考试时间已结束,是否提交?" leftTitle:@"取消" rightTitle:@"确定" cancel:^{
  278. [weakSelf removeAllFile];
  279. [weakSelf.navigationController popViewControllerAnimated:YES];
  280. } confirm:^{
  281. [weakSelf submitService];
  282. }];
  283. }
  284. }
  285. }
  286. - (RecordTipsView *)tipsView {
  287. if (!_tipsView) {
  288. _tipsView = [RecordTipsView shareInstance];
  289. }
  290. return _tipsView;
  291. }
  292. - (UITableView *)tableView {
  293. if (!_tableView) {
  294. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  295. _tableView.delegate = self;
  296. _tableView.dataSource = self;
  297. _tableView.rowHeight = 130;
  298. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  299. _tableView.backgroundColor = HexRGB(0xf5f5f5);
  300. _tableView.showsVerticalScrollIndicator = NO;
  301. [_tableView registerNib:[UINib nibWithNibName:@"RecordListCell" bundle:nil] forCellReuseIdentifier:@"RecordListCell"];
  302. _tableView.tableHeaderView = self.tipsView;
  303. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  304. bottomView.backgroundColor = HexRGB(0xf3f4f8);
  305. _tableView.tableFooterView = bottomView;
  306. }
  307. return _tableView;
  308. }
  309. - (RecordBottomView *)bottomView {
  310. if (!_bottomView) {
  311. _bottomView = [RecordBottomView shareInstance];
  312. _topView.topTitleLabel.text = @"录播模拟考试";
  313. MJWeakSelf;
  314. [_bottomView submitMediaAction:^{
  315. [weakSelf submitAction];
  316. }];
  317. }
  318. return _bottomView;
  319. }
  320. - (void)submitAction {
  321. // 是否确认提交
  322. MJWeakSelf;
  323. [KSNormalAlertView ks_showAlertWithTitle:@"请确认已上传所有录播曲目" leftTitle:@"取消" rightTitle:@"确认" cancel:^{
  324. } confirm:^{
  325. [weakSelf submitService];
  326. }];
  327. }
  328. // 完成考试
  329. - (void)submitService {
  330. [self removeAllFile];
  331. MJWeakSelf;
  332. [self KSShowMsg:@"提交成功" promptCompletion:^{
  333. // 移除所有缓存
  334. UserDefaultRemoveObjectForKey(@"SimulationRecordEndTime");
  335. [weakSelf.topView stopDurationTimer];
  336. [weakSelf.navigationController popViewControllerAnimated:YES];
  337. }];
  338. }
  339. - (void)removeAllFile {
  340. for (NSInteger i = 0; i < self.songArray.count; i++) {
  341. SongModel *model = self.songArray[i];
  342. NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName];
  343. NSDictionary *fileMessage = UserDefault(fileKey);
  344. if (fileMessage) {
  345. NSString *localUrl = [fileMessage stringValueForKey:@"localFileUrl"];
  346. UserDefaultRemoveObjectForKey(fileKey);
  347. [self removeVideoWithPath:localUrl];
  348. [self.fileUrlArray removeObject:localUrl];
  349. }
  350. }
  351. }
  352. - (void)removeVideoWithPath:(NSString *)videoUrl {
  353. NSFileManager *fileMamager = [NSFileManager defaultManager];
  354. if ([fileMamager fileExistsAtPath:videoUrl]) {
  355. [fileMamager removeItemAtPath:videoUrl error:nil];
  356. }
  357. }
  358. - (NSMutableArray *)fileUrlArray {
  359. if (!_fileUrlArray) {
  360. _fileUrlArray = [NSMutableArray arrayWithCapacity:self.songArray.count];
  361. }
  362. return _fileUrlArray;
  363. }
  364. #pragma mark ------- 播放文件
  365. - (void)playVideoWithUrl:(NSURL *)fileUrl {
  366. _playerFrame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin);
  367. _wmPlayer = [[WMPlayer alloc] initWithFrame:_playerFrame];
  368. WMPlayerModel *playModel = [[WMPlayerModel alloc] init];
  369. playModel.videoURL = fileUrl;
  370. _wmPlayer.playerModel = playModel;
  371. _wmPlayer.delegate = self;
  372. _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
  373. _bgView.backgroundColor = [UIColor blackColor];
  374. [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
  375. [[UIApplication sharedApplication].keyWindow addSubview:_wmPlayer];
  376. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:_wmPlayer];
  377. [_wmPlayer play];
  378. }
  379. - (void)wmplayer:(WMPlayer *)wmplayer clickedCloseButton:(UIButton *)backBtn {
  380. [wmplayer removePlayer];
  381. [_bgView removeFromSuperview];
  382. [self setNeedsStatusBarAppearanceUpdate];
  383. }
  384. - (void)wmplayer:(WMPlayer *)wmplayer clickedFullScreenButton:(UIButton *)fullScreenBtn {
  385. self.isRatation = !self.isRatation;
  386. if (self.isRatation) {
  387. [wmplayer removeFromSuperview];
  388. [UIView animateWithDuration:1.0f animations:^{
  389. wmplayer.transform = CGAffineTransformMakeRotation(M_PI_2);
  390. } completion:^(BOOL finished) {
  391. wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin);
  392. [[UIApplication sharedApplication].keyWindow addSubview:wmplayer];
  393. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer];
  394. }];
  395. }
  396. else {
  397. [wmplayer removeFromSuperview];
  398. [UIView animateWithDuration:1.0f animations:^{
  399. // 复原
  400. wmplayer.transform = CGAffineTransformIdentity;
  401. } completion:^(BOOL finished) {
  402. wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin);
  403. [[UIApplication sharedApplication].keyWindow addSubview:wmplayer];
  404. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer];
  405. }];
  406. }
  407. }
  408. - (void)dealloc {
  409. [self.topView stopDurationTimer];
  410. }
  411. /*
  412. #pragma mark - Navigation
  413. // In a storyboard-based application, you will often want to do a little preparation before navigation
  414. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  415. // Get the new view controller using [segue destinationViewController].
  416. // Pass the selected object to the new view controller.
  417. }
  418. */
  419. @end