iOS第三方语音-讯飞语音

官方网站:http://www.xfyun.cn/

注册还要绑定微信,坑啊,识别率感觉没得微信语音好,但是微信语音审核一直不过,研究下这个

1.下载sdk,主要就下面几个文件,我主要用的是语音识别

2.导入相关的库

3.在官网上注册一个应用,得到key,这里用的我注册的key

AppDelegate中导入头文件

#import <iflyMSC/iflyMSC.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中加入

NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"55d3e4d6"];
    [IFlySpeechUtility createUtility:initString];

4.主要分为有UI,和无UI两种IFlySpeechRecognizer(无UI),IFlyRecognizerView(有UI)

无UI代码

 _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance]; //设置听写模式
    [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    [_iFlySpeechRecognizer setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];
    [_iFlySpeechRecognizer setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    _iFlySpeechRecognizer.delegate = self;
#pragma mark - 事件监听
//录音
- (IBAction)startButtonPressed:(UIButton *)sender {
     [_iFlySpeechRecognizer startListening];
//    [_iflyRecognizerView start];
}

//停止
- (IBAction)stopButtonPressed:(UIButton *)sender {
    [_iFlySpeechRecognizer stopListening];
//    [_iflyRecognizerView start];
}
#pragma mark - IFlySpeechRecognizerDelegate


- (void)onResults:(NSArray *)results isLast:(BOOL)isLast {
    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = results[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    NSString *strResult = nil;
    strResult =[NSString stringWithFormat:@"%@",resultString];
    NSString * resultFromJson =  [ISRDataHelper stringFromJson:resultString];

    if (!isLast){
         _label.text = resultFromJson;
    }
    NSLog(@"%@",resultString);
    NSLog(@"resultFromJson=%@",resultFromJson);
}

- (void)onEndOfSpeech {
    NSLog(@"停止录音");
}

- (void)onError:(IFlySpeechError *)errorCode {
    NSLog(@"%d",errorCode.errorCode);
    NSLog(@"%@",[errorCode errorDesc]);
}

有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;
#pragma mark - 事件监听
//录音
- (IBAction)startButtonPressed:(UIButton *)sender {
//     [_iFlySpeechRecognizer startListening];
    [_iflyRecognizerView start];
}
#pragma mark - <IFlyRecognizerViewDelegate>有界面的

- (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast {
    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = resultArray[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    NSString *strResult = nil;
    strResult =[NSString stringWithFormat:@"%@",resultString];
    NSString * resultFromJson =  [ISRDataHelper stringFromJson:resultString];
    
    if (!isLast){
        _label.text = resultFromJson;
    }
    NSLog(@"%@",resultString);
    NSLog(@"resultFromJson=%@",resultFromJson);
}

其中用了一个解析返回的数据的类,可以看讯飞的demo,下面是我写的demo

百度云demo下载链接:http://pan.baidu.com/s/1nt9thKd

补:压缩包的体积好大,郁闷

原文地址:https://www.cnblogs.com/hxwj/p/4741841.html