YBCommentToolBar.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //
  2. // YBCommentToolBar.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/21.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBCommentToolBar.h"
  9. //#import "SelPeopleV.h"
  10. #import "XHVoiceRecordHUD.h"
  11. #import "XHVoiceRecordHelper.h"
  12. #import <AFNetworking/AFNetworking.h>
  13. #import "YBStorageObj.h"
  14. @interface YBCommentToolBar()<HPGrowingTextViewDelegate,twEmojiViewDelegate> {
  15. UIView *_superView;
  16. int _vcType;
  17. // SelPeopleV * _selV;
  18. // twEmojiView *_emojiV;
  19. // NSMutableArray *_atArray; //@用户的uid和uname数组
  20. NSString *qntoken; //七牛token
  21. NSString *tengxunID;
  22. NSString *bucketName;
  23. NSString *regionName;
  24. NSString *voicePathhh;
  25. // NSString *_voiceDution;
  26. ZFPlayerPlaybackState _playState;
  27. }
  28. //@property(nonatomic,strong)HPGrowingTextView *textField;
  29. @property(nonatomic,strong)UIButton *atBtn;
  30. //@property(nonatomic,strong)UIButton *faceBtn;
  31. //@property(nonatomic,strong)UIButton *voiceBtn;
  32. //@property(nonatomic,strong)UIButton *voiceRecodeBtn;
  33. @property(nonatomic, strong, readwrite) XHVoiceRecordHUD *voiceRecordHUD;
  34. /**
  35. * 管理录音工具对象
  36. */
  37. @property(nonatomic, strong) XHVoiceRecordHelper *voiceRecordHelper;
  38. @property(nonatomic,strong)NSDictionary *TXSignDic;
  39. @end
  40. @implementation YBCommentToolBar
  41. - (instancetype)initWithFrame:(CGRect)frame andVCType:(int)vcType superView:(UIView *)superView{
  42. self = [super initWithFrame:frame];
  43. if (self) {
  44. _vcType = vcType;
  45. _superView = superView;
  46. _atArray = [NSMutableArray array];
  47. [self createUI];
  48. }
  49. return self;
  50. }
  51. -(void)createUI {
  52. //设置输入框
  53. UIView *vc = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
  54. vc.backgroundColor = [UIColor clearColor];
  55. _textField = [[HPGrowingTextView alloc]initWithFrame:CGRectMake(44,8, _window_width - 68-44*2, 34)];
  56. _textField.layer.masksToBounds = YES;
  57. _textField.layer.cornerRadius = 17;
  58. _textField.font = SYS_Font(16);
  59. _textField.placeholder = YZMsg(@"说点什么吧~");
  60. _textField.textColor = UIColor.blackColor;
  61. _textField.placeholderColor = UIColor.grayColor;
  62. _textField.delegate = self;
  63. _textField.returnKeyType = UIReturnKeySend;
  64. _textField.enablesReturnKeyAutomatically = YES;
  65. _textField.internalTextView.textContainer.lineBreakMode = NSLineBreakByTruncatingHead;
  66. _textField.internalTextView.textContainer.maximumNumberOfLines = 1;
  67. /**
  68. * 由于 _textField 设置了contentInset 后有色差,在_textField后添
  69. * 加一个背景view并把_textField设置clearColor
  70. */
  71. _textField.contentInset = UIEdgeInsetsMake(2, 10, 2, 10);
  72. _textField.backgroundColor = [UIColor clearColor];
  73. _tv_bg = [[UIView alloc]initWithFrame:_textField.frame];
  74. _tv_bg.backgroundColor = RGB_COLOR(@"#2C2840", 0.2);//RGB(245, 245, 245);//
  75. _tv_bg.layer.masksToBounds = YES;
  76. _tv_bg.layer.cornerRadius = _textField.layer.cornerRadius;
  77. [self addSubview:_tv_bg];
  78. [self addSubview:_textField];
  79. //@功能
  80. _atBtn = [UIButton buttonWithType:0];
  81. _atBtn.backgroundColor = [UIColor clearColor];
  82. _atBtn.frame = CGRectMake(_window_width - 44-10-34,8,34,34);
  83. [_atBtn setImage:[UIImage imageNamed:@"@符"] forState:0];
  84. [_atBtn addTarget:self action:@selector(atFrends) forControlEvents:UIControlEventTouchUpInside];
  85. [self addSubview:_atBtn];
  86. //表情
  87. _faceBtn = [UIButton buttonWithType:0];
  88. _faceBtn.frame = CGRectMake(_window_width - 44,8,34,34);
  89. [_faceBtn setImage:[UIImage imageNamed:@"chat_face.png"] forState:0];
  90. [_faceBtn setImage:[UIImage imageNamed:@"chat_keyboard"] forState:UIControlStateSelected];
  91. [_faceBtn addTarget:self action:@selector(clickFaceBtn) forControlEvents:UIControlEventTouchUpInside];
  92. [self addSubview:_faceBtn];
  93. _faceBtn.selected = NO;
  94. //@声音
  95. _voiceBtn = [UIButton buttonWithType:0];
  96. _voiceBtn.backgroundColor = [UIColor clearColor];
  97. _voiceBtn.frame = CGRectMake(7,8,34,34);
  98. [_voiceBtn setImage:[UIImage imageNamed:@"video_voice_nor"] forState:0];
  99. [_voiceBtn setImage:[UIImage imageNamed:@"video_voice_sel"] forState:UIControlStateSelected];
  100. [_voiceBtn addTarget:self action:@selector(switchVoice) forControlEvents:UIControlEventTouchUpInside];
  101. [self addSubview:_voiceBtn];
  102. _voiceRecodeBtn = [UIButton buttonWithType:0];
  103. _voiceRecodeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  104. _voiceRecodeBtn.frame = _tv_bg.frame;
  105. [_voiceRecodeBtn setTitle:YZMsg(@"按住说话") forState:UIControlStateNormal];
  106. [_voiceRecodeBtn setTitle:YZMsg(@"松开结束") forState:UIControlStateHighlighted];
  107. _voiceRecodeBtn.layer.masksToBounds = YES;
  108. _voiceRecodeBtn.layer.cornerRadius = 18;
  109. _voiceRecodeBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
  110. [_voiceRecodeBtn setBackgroundColor:RGB_COLOR(@"#EA377F", 1)];
  111. [_voiceRecodeBtn addTarget:self action:@selector(holdDownButtonTouchDown) forControlEvents:UIControlEventTouchDown];
  112. [_voiceRecodeBtn addTarget:self action:@selector(holdDownButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
  113. [_voiceRecodeBtn addTarget:self action:@selector(holdDownButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  114. [_voiceRecodeBtn addTarget:self action:@selector(holdDownDragOutside) forControlEvents:UIControlEventTouchDragExit];
  115. [_voiceRecodeBtn addTarget:self action:@selector(holdDownDragInside) forControlEvents:UIControlEventTouchDragEnter];
  116. [self addSubview:_voiceRecodeBtn];
  117. _voiceRecodeBtn.hidden = YES;
  118. //表情
  119. _emojiV = [[twEmojiView alloc]initWithFrame:CGRectMake(0, _window_height, _window_width, EmojiHeight+ShowDiff)];
  120. _emojiV.delegate = self;
  121. // [[YBBaseAppDelegate sharedAppDelegate].topViewController.view addSubview:_emojiV];
  122. [_superView addSubview:_emojiV];
  123. //游客加一层遮罩,点击直接跳登陆
  124. if ([[Config getOwnID] intValue]<0) {
  125. UIButton *shadowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  126. [shadowBtn addTarget:self action:@selector(clickWarningLogin) forControlEvents:UIControlEventTouchUpInside];
  127. shadowBtn.backgroundColor = UIColor.clearColor;
  128. [self addSubview:shadowBtn];
  129. [shadowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  130. make.width.height.centerX.centerY.equalTo(self);
  131. }];
  132. }
  133. }
  134. -(void)clickWarningLogin {
  135. if (self.comToolEvent) {
  136. self.comToolEvent(@"工具-游客", @{});
  137. }
  138. }
  139. #pragma mark - 召唤好友
  140. -(void)atFrends {
  141. [_textField resignFirstResponder];
  142. _isPrepareEditing = NO;
  143. [self resetVoiceBtn];
  144. YBWeakSelf;
  145. if (!_selV) {
  146. _selV = [[SelPeopleV alloc]initWithFrame:CGRectMake(0, _window_height, _window_width, _window_height) showType:@"2" selUser:^(NSString *state, MessageListModel *userModel) {
  147. [weakSelf selCallBack:state uModel:userModel];
  148. }];
  149. [_superView addSubview:_selV];
  150. }
  151. [UIView animateWithDuration:0.25 animations:^{
  152. _selV.frame = CGRectMake(0, 0, _window_width, _window_height);
  153. }];
  154. }
  155. -(void)selCallBack:(NSString *)state uModel:(MessageListModel *)model{
  156. if ([state isEqual:@"关闭"]) {
  157. [UIView animateWithDuration:0.25 animations:^{
  158. _selV.frame = CGRectMake(0, _window_height, _window_width, _window_height);
  159. } completion:^(BOOL finished) {
  160. [_selV removeFromSuperview];
  161. _selV = nil;
  162. }];
  163. }else {
  164. //@
  165. [UIView animateWithDuration:0.25 animations:^{
  166. _selV.frame = CGRectMake(0,_window_height, _window_width, _window_height);
  167. } completion:^(BOOL finished) {
  168. [_selV removeFromSuperview];
  169. _selV = nil;
  170. BOOL isRepeat = NO;
  171. for (NSDictionary *subAtDic in _atArray) {
  172. NSString *subUid = [NSString stringWithFormat:@"%@",[subAtDic valueForKey:@"uid"]];
  173. if ([model.uidStr isEqual:subUid]) {
  174. //有重复
  175. isRepeat = YES;
  176. }
  177. }
  178. //输入框显示用户
  179. if (isRepeat==NO) {
  180. _textField.text = [NSString stringWithFormat:@"%@@%@ ",_textField.text,model.unameStr];
  181. NSDictionary *dic = @{@"name":model.unameStr,@"uid":model.uidStr};
  182. [_atArray addObject:dic];
  183. }
  184. NSLog(@"===输入框显示用户===%@==%@===con:%@",model.uidStr,model.unameStr,_textField.text);
  185. }];
  186. }
  187. [_textField becomeFirstResponder];
  188. _faceBtn.selected = NO;
  189. _isPrepareEditing = YES;
  190. }
  191. #pragma mark - 输入框代理事件
  192. - (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
  193. _textField.height = height;
  194. }
  195. - (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView {
  196. [_textField resignFirstResponder];
  197. _isPrepareEditing = NO;
  198. [self pushmessage:nil];
  199. return YES;
  200. }
  201. - (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  202. if ([text isEqualToString:@""]) {
  203. NSRange selectRange = growingTextView.selectedRange;
  204. if (selectRange.length > 0) {
  205. //用户长按选择文本时不处理
  206. return YES;
  207. }
  208. // 判断删除的是一个@中间的字符就整体删除
  209. NSMutableString *string = [NSMutableString stringWithString:growingTextView.text];
  210. NSArray *matches = [self findAllAt];
  211. BOOL inAt = NO;
  212. NSString *delName = @"";
  213. NSInteger index = range.location;
  214. for (NSTextCheckingResult *match in matches) {
  215. NSRange newRange = NSMakeRange(match.range.location + 1, match.range.length - 1);
  216. if (NSLocationInRange(range.location, newRange)) {
  217. //删除的人名称
  218. delName = [string substringWithRange:newRange];
  219. inAt = YES;
  220. index = match.range.location;
  221. [string replaceCharactersInRange:match.range withString:@""];
  222. break;
  223. }
  224. }
  225. if (inAt) {
  226. //删除是输入框中@的人,atArray中也要移除这个人
  227. NSMutableArray *del_m = [NSMutableArray array];
  228. for (NSDictionary *subDic in _atArray) {
  229. if ([delName isEqual:minstr([subDic valueForKey:@"name"])]) {
  230. [del_m addObject:subDic];
  231. }
  232. }
  233. [_atArray removeObjectsInArray:del_m];
  234. growingTextView.text = string;
  235. growingTextView.selectedRange = NSMakeRange(index, 0);
  236. return NO;
  237. }
  238. }
  239. //判断是回车键就发送出去
  240. if ([text isEqualToString:@"\n"]) {
  241. [self pushmessage:nil];
  242. return NO;
  243. }
  244. return YES;
  245. }
  246. - (void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView {
  247. UITextRange *selectedRange = growingTextView.internalTextView.markedTextRange;
  248. NSString *newText = [growingTextView.internalTextView textInRange:selectedRange];
  249. if (newText.length < 1) {
  250. // 高亮输入框中的@
  251. UITextView *textView = _textField.internalTextView;
  252. NSRange range = textView.selectedRange;
  253. NSMutableAttributedString *string;
  254. if(_vcType == 0){
  255. string= [[NSMutableAttributedString alloc] initWithString:textView.text attributes:@{NSForegroundColorAttributeName:UIColor.whiteColor,NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  256. }else{
  257. string= [[NSMutableAttributedString alloc] initWithString:textView.text attributes:@{NSForegroundColorAttributeName:RGB_COLOR(@"#969696", 1),NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  258. }
  259. NSArray *matches = [self findAllAt];
  260. for (NSTextCheckingResult *match in matches) {
  261. [string addAttribute:NSForegroundColorAttributeName value:AtCol range:NSMakeRange(match.range.location, match.range.length - 1)];
  262. }
  263. textView.attributedText = string;
  264. textView.selectedRange = range;
  265. }
  266. if (growingTextView.text.length >0) {
  267. NSString *theLast = [growingTextView.text substringFromIndex:[growingTextView.text length]-1];
  268. if ([theLast isEqual:@"@"]) {
  269. //去掉手动输入的 @
  270. NSString *end_str = [growingTextView.text substringToIndex:growingTextView.text.length-1];
  271. _textField.text = end_str;
  272. [self atFrends];
  273. }
  274. }
  275. }
  276. - (void)growingTextViewDidChangeSelection:(HPGrowingTextView *)growingTextView {
  277. // 光标不能点落在@词中间
  278. NSRange range = growingTextView.selectedRange;
  279. if (range.length > 0) {
  280. // 选择文本时可以
  281. return;
  282. }
  283. NSArray *matches = [self findAllAt];
  284. for (NSTextCheckingResult *match in matches) {
  285. NSRange newRange = NSMakeRange(match.range.location + 1, match.range.length - 1);
  286. if (NSLocationInRange(range.location, newRange)) {
  287. growingTextView.internalTextView.selectedRange = NSMakeRange(match.range.location + match.range.length, 0);
  288. break;
  289. }
  290. }
  291. }
  292. #pragma mark - Private
  293. - (NSArray<NSTextCheckingResult *> *)findAllAt {
  294. // 找到文本中所有的@
  295. NSString *string = _textField.text;
  296. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:kATRegular options:NSRegularExpressionCaseInsensitive error:nil];
  297. NSArray *matches = [regex matchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, [string length])];
  298. return matches;
  299. }
  300. -(void)pushmessage:(NSString *)voiceKey{
  301. //发送消息区分:YBLookVideoVC 底部还是 评论view(commentview) 来发送消息
  302. if (_vcType == 1) {
  303. if (self.comToolEvent) {
  304. self.comToolEvent(@"工具-发送消息", @{@"voiceKey":voiceKey?voiceKey:@""});
  305. }
  306. [MBProgressHUD hideHUD];
  307. return;
  308. }
  309. if ([[Config getOwnID] intValue] <= 0) {
  310. [_textField resignFirstResponder];
  311. _isPrepareEditing = NO;
  312. [PublicObj warnLogin];
  313. [MBProgressHUD hideHUD];
  314. return;
  315. }
  316. /*
  317. parentid 回复的评论ID
  318. commentid 回复的评论commentid
  319. touid 回复的评论UID
  320. 如果只是评论 这三个传0
  321. */
  322. if (_textField.text.length == 0 && voiceKey == nil) {
  323. [MBProgressHUD showError:YZMsg(@"请添加内容后再尝试")];
  324. [MBProgressHUD hideHUD];
  325. return;
  326. }
  327. NSString *path = _textField.text;
  328. NSString *at_json = @"";
  329. //转json、去除空格、回车
  330. if (_atArray.count>0) {
  331. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:_atArray options:NSJSONWritingPrettyPrinted error:nil];
  332. at_json = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  333. at_json = [at_json stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  334. at_json = [at_json stringByReplacingOccurrencesOfString:@" " withString:@""];
  335. at_json = [at_json stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  336. }
  337. /*
  338. NSString *url = [NSString stringWithFormat:@"Video.setComment&videoid=%@&content=%@&uid=%@&token=%@&touid=%@&commentid=%@&parentid=%@&at_info=%@&type=%@&voice=%@&length=%@",_videoid,voiceKey?@"":path,[Config getOwnID],[Config getOwnToken],_hostid,@"0",@"0",at_json,voiceKey?@"1":@"0",voiceKey,voiceKey?_voiceDution:@"0"];
  339. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  340. */
  341. NSString *url = @"Video.setComment";
  342. NSDictionary *postDic = @{@"videoid":_videoid,
  343. @"content":voiceKey?@"":path,
  344. @"touid":_hostid,
  345. @"commentid":@"0",
  346. @"parentid":@"0",
  347. @"at_info":at_json,
  348. @"type":voiceKey?@"1":@"0",
  349. @"voice":voiceKey?voiceKey:@"",
  350. @"length":voiceKey?_voiceDution:@"0",
  351. };
  352. YBWeakSelf;
  353. [YBNetworking postWithUrl:url Dic:postDic Suc:^(int code, id info, NSString *msg) {
  354. [MBProgressHUD hideHUD];
  355. if(code == 0){
  356. //更新评论数量
  357. NSDictionary *infoDic = [info firstObject];
  358. NSString *newComments = [NSString stringWithFormat:@"%@",[infoDic valueForKey:@"comments"]];
  359. if (weakSelf.comToolEvent) {
  360. weakSelf.comToolEvent(@"工具-评论", @{@"comments":newComments});
  361. }
  362. [MBProgressHUD showPop:msg];
  363. weakSelf.textField.text = @"";
  364. [_atArray removeAllObjects];
  365. weakSelf.textField.placeholder = YZMsg(@"说点什么吧~");
  366. [_superView endEditing:YES];
  367. [self hiddenEmojiOrKeyBoard];
  368. }else{
  369. [MBProgressHUD showPop:msg];
  370. weakSelf.textField.text = @"";
  371. [_atArray removeAllObjects];
  372. weakSelf.textField.placeholder = YZMsg(@"说点什么吧~");
  373. [_superView endEditing:YES];
  374. [self hiddenEmojiOrKeyBoard];
  375. }
  376. } Fail:^(id fail) {
  377. [MBProgressHUD hideHUD];
  378. }];
  379. }
  380. #pragma mark - 显示表情
  381. -(void)clickFaceBtn {
  382. [self resetVoiceBtn];
  383. if (_faceBtn.selected == NO) {
  384. _faceBtn.selected = YES;
  385. [_textField resignFirstResponder];
  386. [UIView animateWithDuration:0.25 animations:^{
  387. _emojiV.frame = CGRectMake(0, _window_height - (EmojiHeight+ShowDiff), _window_width, EmojiHeight+ShowDiff);
  388. self.frame = CGRectMake(0, _emojiV.y - 50, _window_width, 50);
  389. }];
  390. }else{
  391. _faceBtn.selected = NO;
  392. [_textField becomeFirstResponder];
  393. }
  394. _isPrepareEditing = YES;
  395. }
  396. #pragma mark - Emoji 代理
  397. -(void)sendimage:(NSString *)str {
  398. if ([str isEqual:@"msg_del"]) {
  399. [_textField.internalTextView deleteBackward];
  400. }else {
  401. [_textField.internalTextView insertText:str];
  402. }
  403. }
  404. -(void)clickSendEmojiBtn {
  405. [self hiddenEmojiOrKeyBoard];
  406. [self pushmessage:nil];
  407. }
  408. -(void)hiddenEmojiOrKeyBoard {
  409. [_superView endEditing:YES];
  410. _isPrepareEditing = NO;
  411. _faceBtn.selected = NO;
  412. [UIView animateWithDuration:0.25 animations:^{
  413. self.frame = CGRectMake(0, _window_height - 50-ShowDiff, _window_width, 50+ShowDiff);
  414. _emojiV.frame = CGRectMake(0, _window_height, _window_width, EmojiHeight+ShowDiff);
  415. }];
  416. }
  417. #pragma mark ============录音=============
  418. - (void)switchVoice{
  419. _voiceBtn.selected = !_voiceBtn.selected;
  420. _voiceRecodeBtn.hidden = !_voiceBtn.selected;
  421. _textField.hidden = _voiceBtn.selected;
  422. [self hiddenEmojiOrKeyBoard];
  423. }
  424. -(void)resetVoiceBtn {
  425. [_voiceRecodeBtn setTitle:YZMsg(@"按住说话") forState:UIControlStateNormal];
  426. [_voiceRecodeBtn setTitle:YZMsg(@"松开结束") forState:UIControlStateHighlighted];
  427. _voiceBtn.selected = NO;
  428. _voiceRecodeBtn.hidden = !_voiceBtn.selected;
  429. _textField.hidden = _voiceBtn.selected;
  430. }
  431. -(void)resetToolState {
  432. [_textField resignFirstResponder];
  433. _textField.text = @"";
  434. _isPrepareEditing = NO;
  435. [self resetVoiceBtn];
  436. }
  437. - (void)holdDownButtonTouchDown {
  438. DDLogVerbose(@"Action - didStartRecordingVoice");
  439. //开始录音,记录当前视频播放器的状态
  440. _playState = self.player.currentPlayerManager.playState;
  441. [self setVideoMuted:YES];
  442. [self startRecord];
  443. }
  444. - (void)holdDownButtonTouchUpOutside {
  445. DDLogVerbose(@"Action - didCancelRecordingVoice");
  446. [self setVideoMuted:NO];
  447. [self cancelRecord];
  448. }
  449. - (void)holdDownButtonTouchUpInside {
  450. DDLogVerbose(@"Action - didFinishRecordingVoiceAction");
  451. [self setVideoMuted:NO];
  452. [self finishRecorded];
  453. }
  454. - (void)holdDownDragOutside {
  455. DDLogVerbose(@"Action - didDragOutsideAction");
  456. [self resumeRecord];
  457. }
  458. - (void)holdDownDragInside {
  459. DDLogVerbose(@"Action - didDragInsideAction");
  460. [self pauseRecord];
  461. }
  462. - (void)pauseRecord {
  463. [self.voiceRecordHUD pauseRecord];
  464. }
  465. - (void)resumeRecord {
  466. [self.voiceRecordHUD resaueRecord];
  467. }
  468. - (void)cancelRecord {
  469. YBWeakSelf;
  470. [self.voiceRecordHUD cancelRecordCompled:^(BOOL fnished) {
  471. __strong __typeof(weakSelf)strongSelf = weakSelf;
  472. strongSelf.voiceRecordHUD = nil;
  473. }];
  474. [self.voiceRecordHelper cancelledDeleteWithCompletion:^{
  475. }];
  476. }
  477. #pragma mark - Voice Recording Helper Method
  478. - (void)startRecord {
  479. DDLogDebug(@"Action - startRecord");
  480. [self.voiceRecordHUD startRecordingHUDAtView:_superView];
  481. [self.voiceRecordHelper startRecordingWithPath:[self getRecorderPath] andVideo:YES StartRecorderCompletion:^{
  482. }];
  483. }
  484. - (XHVoiceRecordHelper *)voiceRecordHelper {
  485. if (!_voiceRecordHelper) {
  486. YBWeakSelf;
  487. _voiceRecordHelper = [[XHVoiceRecordHelper alloc] init];
  488. _voiceRecordHelper.maxTimeStopRecorderCompletion = ^{
  489. DDLogDebug(@"已经达到最大限制时间了,进入下一步的提示");
  490. __strong __typeof(weakSelf)strongSelf = weakSelf;
  491. [strongSelf finishRecorded];
  492. };
  493. _voiceRecordHelper.peakPowerForChannel = ^(float peakPowerForChannel) {
  494. __strong __typeof(weakSelf)strongSelf = weakSelf;
  495. strongSelf.voiceRecordHUD.peakPower = peakPowerForChannel;
  496. };
  497. _voiceRecordHelper.maxRecordTime = 60;
  498. }
  499. return _voiceRecordHelper;
  500. }
  501. - (XHVoiceRecordHUD *)voiceRecordHUD {
  502. if (!_voiceRecordHUD) {
  503. _voiceRecordHUD = [[XHVoiceRecordHUD alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
  504. }
  505. return _voiceRecordHUD;
  506. }
  507. - (void)finishRecorded {
  508. DDLogDebug(@"Action - finishRecorded");
  509. YBWeakSelf;
  510. [self.voiceRecordHUD stopRecordCompled:^(BOOL fnished) {
  511. __strong __typeof(weakSelf)strongSelf = weakSelf;
  512. strongSelf.voiceRecordHUD = nil;
  513. }];
  514. [self.voiceRecordHelper stopRecordingWithStopRecorderCompletion:^{
  515. __strong __typeof(weakSelf)strongSelf = weakSelf;
  516. [strongSelf SendMessageWithVoice:strongSelf.voiceRecordHelper.recordPath
  517. voiceDuration:strongSelf.voiceRecordHelper.recordDuration];
  518. }];
  519. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  520. }
  521. #pragma mark - Message Send helper Method
  522. #pragma mark --发送语音
  523. - (void)SendMessageWithVoice:(NSString *)voicePath
  524. voiceDuration:(NSString*)voiceDuration {
  525. DDLogDebug(@"Action - SendMessageWithVoice");
  526. if (voicePath == voicePathhh) {
  527. return;
  528. }
  529. if ([voiceDuration integerValue]<0.5 || [voiceDuration integerValue]>60) {
  530. if ([voiceDuration integerValue]<0.5) {
  531. DDLogDebug(@"录音时长小于 0.5s");
  532. [MBProgressHUD showError:YZMsg(@"录音时间过短")];
  533. } else {
  534. DDLogDebug(@"录音时长大于 60s");
  535. [MBProgressHUD showError:YZMsg(@"录音时长大于 60s")];
  536. }
  537. return;
  538. }
  539. voicePathhh = voicePath;
  540. _voiceDution = voiceDuration;
  541. [self clickPublishBtn];
  542. }
  543. #pragma mark - RecorderPath Helper Method
  544. - (NSString *)getRecorderPath {
  545. NSString *recorderPath = nil;
  546. NSDate *now = [NSDate date];
  547. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  548. dateFormatter.dateFormat = @"yy-MMMM-dd";
  549. recorderPath = [[NSString alloc] initWithFormat:@"%@/Documents/", NSHomeDirectory()];
  550. dateFormatter.dateFormat = @"yyyy-MM-dd-hh-mm-ss";
  551. recorderPath = [recorderPath stringByAppendingFormat:@"%@-voiceComment.wav", [dateFormatter stringFromDate:now]];
  552. return recorderPath;
  553. }
  554. - (void)setVideoMuted:(BOOL)isMuted{
  555. if (self.comToolEvent) {
  556. self.comToolEvent(@"工具-录音", @{@"mutedType":@(isMuted),@"playState":@(_playState)});
  557. }
  558. }
  559. #pragma mark - 发布
  560. - (void)clickPublishBtn {
  561. [_superView endEditing:YES];
  562. _isPrepareEditing = NO;
  563. YBWeakSelf;
  564. [[YBStorageObj storageManeger]getCOSType:^(int code) {
  565. if (code == 0) {
  566. [weakSelf startUpload];
  567. }
  568. }];
  569. }
  570. -(void)startUpload {
  571. NSString *filePath = voicePathhh;
  572. //传视频
  573. NSString *voiceoName = [PublicObj getNameBaseCurrentTime:@".wav"];
  574. YBWeakSelf;
  575. [MBProgressHUD showMessage:@""];
  576. [[YBStorageObj storageManeger]yb_storageVideoOrVoice:filePath andName:voiceoName progress:^(CGFloat percent) {
  577. }complete:^(int code, NSString *key) {
  578. dispatch_async(dispatch_get_main_queue(), ^{
  579. [MBProgressHUD hideHUD];
  580. });
  581. if (code == 0) {
  582. dispatch_async(dispatch_get_main_queue(), ^{
  583. [weakSelf pushmessage:key];
  584. });
  585. }else {
  586. dispatch_async(dispatch_get_main_queue(), ^{
  587. [MBProgressHUD showPop:YZMsg(@"添加失败")];
  588. });
  589. }
  590. }];
  591. }
  592. -(void)networkState{
  593. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  594. switch (status) {
  595. case AFNetworkReachabilityStatusNotReachable:
  596. [MBProgressHUD hideHUD];
  597. [MBProgressHUD showError:YZMsg(@"上传失败")];
  598. break;
  599. default:
  600. break;
  601. }
  602. }];
  603. [[AFNetworkReachabilityManager sharedManager] startMonitoring]; //开启网络监控
  604. }
  605. @end