一、前言
最近IM即时通讯工具老罗发布一款产品叫做子弹短信,碰巧公司也有这个需求,于是乎,结合了科大讯飞和 强大工具 EZAudio,感谢该工具作者提供!githua传送门: https://github.com/syedhali/EZAudio ,下面我将自己的Demo 和实现过程简单描述下~
二、使用
// 弹出视图
- (IBAction)showManager:(id)sender {
UIButton *btn = (UIButton *)sender;
CGPoint point = CGPointMake(btn.center.x,btn.frame.origin.y);
_ssss = [[ATRecordToolView alloc] initWithOrigin:point Width:ScreenWidth -100 Height:141 Type:XTTypeOfRightCenter Color:[UIColor clearColor]];
[_ssss popView];
}
//在ATRecordToolView 点h里面 申明Xib的这个View
// backGoundView
@property (nonatomic, strong) ATRecordView * _Nonnull backGoundView;
//初始化
- (instancetype)initWithOrigin:(CGPoint)origin Width:(CGFloat)width Height:(CGFloat)height Type:(XTDirectionType)type Color:(UIColor *)color
{
self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.origin = origin;
self.width = width;
self.height = height;
self.type = type;
self.backGoundView = LOADNIBWITHNAME(@"ATRecordView", nil);
self.backGoundView.frame = CGRectMake(origin.x, origin.y, width, height);
[self.backGoundView setComple:^{
[self.iFlySpeechRecognizer stopListening];
[self dismiss];
NSLog(@"最终识别的文字%@",self.backGoundView.textView.text);
//录音文件暂未实现
}];
[self initRecognizer];
[self.backGoundView setCancelBlock:^{
[self.iFlySpeechRecognizer stopListening];
}];
self.backGoundView.textView.delegate =self;
[self addSubview:self.backGoundView];
[self startRecrod]; //初始化科大讯飞音频流
[self startAnmition];//初始化声波特效
}
return self;
}
- (void)startAnmition{
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (error)
{
NSLog(@"Error setting up audio session category: %@", error.localizedDescription);
}
[session setActive:YES error:&error];
if (error)
{
NSLog(@"Error setting up audio session active: %@", error.localizedDescription);
}
self.backGoundView.audioPlot.backgroundColor = [UIColor clearColor];
//
// Waveform color
//
self.backGoundView.audioPlot.color = [UIColor colorWithRed:186/255.0 green:186/255.0 blue:186/255.0 alpha:1];
self.backGoundView.audioPlot.plotType = EZPlotTypeRolling;
self.backGoundView.audioPlot.shouldFill = YES;
self.backGoundView.audioPlot.shouldMirror = YES;
self.backGoundView.microphone = [EZMicrophone microphoneWithDelegate:self];
[self.backGoundView.microphone setDevice:[EZAudioDevice inputDevices][0]];
//
// Start the microphone
//
[self.backGoundView.microphone startFetchingAudio];
}
- (void)startRecrod{
[_backGoundView.textView setText:@""];
[_backGoundView.textView resignFirstResponder];
NSLog(@"%s[IN]",__func__);
if(_iFlySpeechRecognizer == nil)
{
[self initRecognizer];
}
[_iFlySpeechRecognizer setDelegate:self];
[_iFlySpeechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]];
[_iFlySpeechRecognizer setParameter:IFLY_AUDIO_SOURCE_STREAM forKey:@"audio_source"]; //Set audio stream as audio source,which requires the developer import audio data into the recognition control by self through "writeAudio:".
BOOL ret = [_iFlySpeechRecognizer startListening];
if (ret) {
//set the category of AVAudioSession
[IFlyAudioSession initRecordingAudioSession];
_pcmRecorder.delegate = self;
//start recording
BOOL ret = [_pcmRecorder start];
NSLog(@"%s[OUT],Success,Recorder ret=%d",__func__,ret);
}
else
{
NSLog(@"%s[OUT],Failed",__func__);
}
}
部分代码粘贴出来,是为了大致的一个实现过程;具体看Demo哈~.对你有帮助,给点支持!
Demo地址 https://github.com/SwiftHsw/IMRecordTool
最终效果图