NTESAudio2TextViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // NTESAudio2TextViewController.m
  3. // NIM
  4. //
  5. // Created by amao on 7/10/15.
  6. // Copyright (c) 2015 Netease. All rights reserved.
  7. //
  8. #import "NTESAudio2TextViewController.h"
  9. #import "UIView+Toast.h"
  10. #import "SVProgressHUD.h"
  11. #import "UIView+NTES.h"
  12. //#import "NTESSessionViewController.h"
  13. //#import "NTESMainTabController.h"
  14. @interface NTESAudio2TextViewController ()
  15. @property (weak, nonatomic) IBOutlet UITextView *textView;
  16. @property (strong, nonatomic) NIMAudioToTextOption *option;
  17. @property (strong, nonatomic) NIMMessage *message;
  18. @end
  19. @implementation NTESAudio2TextViewController
  20. - (instancetype)initWithMessage:(NIMMessage *)message
  21. {
  22. if (self = [super initWithNibName:@"NTESAudio2TextViewController"
  23. bundle:nil])
  24. {
  25. NIMAudioToTextOption *option = [[NIMAudioToTextOption alloc] init];
  26. option.url = [(NIMAudioObject *)message.messageObject url];
  27. option.filepath = [(NIMAudioObject *)message.messageObject path];
  28. _option = option;
  29. _message = message;
  30. }
  31. return self;
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. [SVProgressHUD showWithStatus:@"正在转换"];
  36. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
  37. [self.textView addGestureRecognizer:tap];
  38. __weak typeof(self) weakSelf = self;
  39. [[[NIMSDK sharedSDK] mediaManager] transAudioToText:_option
  40. result:^(NSError *error, NSString *text) {
  41. [SVProgressHUD dismiss];
  42. weakSelf.cancelBtn.hidden = YES;
  43. [weakSelf show:error
  44. text:text];
  45. if (!error) {
  46. weakSelf.message.isPlayed = YES;
  47. }else{
  48. DDLogError(@"audio 2 text error, %@",error);
  49. [weakSelf.textView removeFromSuperview];
  50. [weakSelf.view addSubview:weakSelf.errorTipView];
  51. }
  52. }];
  53. [_textView setEditable:NO];
  54. }
  55. - (void)viewDidLayoutSubviews{
  56. CGRect rect = CGRectApplyAffineTransform(self.view.frame, self.view.transform);
  57. self.errorTipView.top = rect.size.height * .33f;
  58. self.errorTipView.centerX = rect.size.width * .5f;
  59. }
  60. - (void)show:(NSError *)error
  61. text:(NSString *)text
  62. {
  63. if (error) {
  64. [self.view makeToast:NSLocalizedString(@"转换失败", nil)
  65. duration:2
  66. position:CSToastPositionCenter];
  67. }
  68. else
  69. {
  70. _textView.text = text;
  71. [_textView sizeToFit];
  72. if (self.textView.height + self.textView.top > self.view.height) {
  73. self.textView.height = self.view.height - self.textView.top;
  74. self.textView.scrollEnabled = YES;
  75. }else{
  76. self.textView.scrollEnabled = NO;
  77. }
  78. }
  79. }
  80. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  81. {
  82. [super touchesBegan:touches withEvent:event];
  83. [self hide];
  84. }
  85. - (void)hide{
  86. [SVProgressHUD dismiss];
  87. void (^handler)(void) = self.completeHandler;
  88. [self dismissViewControllerAnimated:NO
  89. completion:^{
  90. if (handler) {
  91. handler();
  92. }
  93. }];
  94. }
  95. - (IBAction)cancelTrans:(id)sender{
  96. [SVProgressHUD dismiss];
  97. [self dismissViewControllerAnimated:NO
  98. completion:nil];
  99. }
  100. - (BOOL)shouldAutorotate{
  101. return NO;
  102. }
  103. @end