本文主要介绍的是利用科大讯飞技术实现语音转文字的功能(语音听写).
首先,注册讯飞账号,申请APPID
然后,下载科大讯飞SDK将开发工具包中lib目录下的iflyMSC.framework添加到新建工程中.
按下图添加SDK所需要的iOS库。(除iflyMSC.framework,iflyMSC.framework是拖进去的)
接着,在appDelegate中引入头文件
#import “iflyMSC/IFlyRecognizerViewDelegate.h”
#import “iflyMSC/IFlyRecognizerView.h”
写上代码
NSString *appid = @"********";//自己申请的appId
NSString *initString = [NSString stringWithFormat:@"appid=%@",appid];
[IFlySpeechUtility createUtility:initString];
在相应的viewController里:
遵守协议<IFlyRecognizerViewDelegate>
@property (nonatomic,strong)IFlyRecognizerView *iflyRecognizerView;
- (void)viewDidLoad {
[super viewDidLoad];
self.iflyRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
self.iflyRecognizerView.delegate = self;
[self.view addSubview:self.iflyRecognizerView];
[self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
//asr_audio_path保存录音文件名,默认目录是documents
[self.iflyRecognizerView setParameter: @"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
//设置返回的数据格式为默认plain
[self.iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
[self startListenning:self.iflyRecognizerView];//可以建一个按钮,点击按钮调用此方法
}
- (void)startListenning:(id)sender{
[self.iflyRecognizerView start];
NSLog(@"开始识别");
}
//返回数据处理
- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast
{
NSMutableString *result = [NSMutableString new];
NSDictionary *dic = [resultArray objectAtIndex:0];
NSLog(@"DIC:%@",dic);
for (NSString *key in dic) {
[result appendFormat:@"%@",key];
}
//把相应的控件赋值为result.例如:label.text = result;
}
- (void)onError:(IFlySpeechError *)error
{
}
这样就实现了利用科大讯飞语音转文字的功能.