为textView添加语音输入功能的实例代码(集成讯飞语音识别)

2020-01-21 04:40:14王旭

非常感谢大家利用自己宝贵的时间来阅读我的文章 , 今天给大家带来的是一个集成讯飞语音识别功能的小demo,为textview添加一个语音输入的功能,相信在这个智能化趋势的大环境的下,很多人能用得到这个功能。如果需要的话希望能帮到你 , 当然, 有任何不妥的地方 欢迎指正。

先上demo --->XunFeiDemo

效果展示:

textView,添加,语音输入,集成讯飞,语音识别

功能实现,sdk中提供了两种方式,一种是带界面的语音识别,有一个识别语音的动画的界面效果。另一种是无界面的。我这里使用的是带界面的,不带界面的自己可以去看一下,大同小异

第一步:去讯飞开发者平台注册账号、创建应用、下载SDK、拖入项目

第二步:添加依赖库


libz.tbd
AVFoundation.framework
SystemConfiguration.framework
Foundation.framework
CoreTelephony.framework
AudioToolbox.framework
UIKit.framework
AddressBook.framework
CoreLocation.framework
CoreGraphics.framework

第三步:AppDelegate配置


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 //存储路径
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
 NSString *cachePath = [paths objectAtIndex:0];
 [IFlySetting setLogFilePath:cachePath];
 //创建语音配置,appid必须要传入,仅执行一次则可
 NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"你的appid"];
 //所有服务启动前,需要确保执行createUtility
 [IFlySpeechUtility createUtility:initString];
}

第四步:把demo里的ISRDataHelper.h及.m文件拖入项目中

第五步:控制器代码

1、引用头文件


#import "iflyMSC/IFlyMSC.h" 
 
#import "ISRDataHelper.h" 

2、初始化_iflyRecognizerView


//有界面
-(void)initRecognizer{
 //单例模式,UI的实例
 if (_iflyRecognizerView == nil) {
  //UI显示剧中
  _iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
  [_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
  //设置听写模式
  [_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
 }
 _iflyRecognizerView.delegate = self;
 if (_iflyRecognizerView != nil) {
  //设置最长录音时间
  [_iflyRecognizerView setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
  //设置后端点 3000
  [_iflyRecognizerView setParameter:@"3000" forKey:[IFlySpeechConstant VAD_EOS]];
  //设置前端点 3000
  [_iflyRecognizerView setParameter:@"3000" forKey:[IFlySpeechConstant VAD_BOS]];
  //设置采样率,推荐使用16K 16000
  [_iflyRecognizerView setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
  //  if ([instance.language isEqualToString:[IATConfig chinese]]) {
  //   //设置语言 zh_cn
  [_iflyRecognizerView setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];
  //   //设置方言 mandarin
  [_iflyRecognizerView setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];
  //  }else if ([instance.language isEqualToString:[IATConfig english]]) {
  //   //设置语言
  //   [_iflyRecognizerView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
  //  }
  //  //设置是否返回标点符号 0
  [_iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
 }
}