iOS: 讯飞语音的使用

一、介绍:

讯飞语音做的相当不错,容错率达到90%多,如果需要做语音方面的功能,它绝对是一个不错的选择。讯飞语音的功能很多:语音听写、语音识别、语音合成等,但我们最常用的还是语音听写。讯飞语音中包含界面的语音听写和不带界面的语音听写,下面我来演示一下。

 

二、准备工作:

(1)去讯飞语音开发平台注册账号并登陆,然后在控制台创建应用,获取对应的app id,这个以后使用它注册激活讯飞语音。

(2)下载讯飞语音SDK,将其拖入到项目中,然后添加需要所有的依赖库,另外还有新添加库Contacts.framework,编译运行即可。

(3)引入头文件,以及遵从代理,激活讯飞语音,并实现功能。

 

三、BitCode 设置

在 Xcode 7 默认开启了 Bitcode,Bitcode 需要工程依赖的类库同时支持。而语记 SDK 暂时还不支持Bitcode,所以可以先临时关闭。后续支持 Bitcode 请关注讯飞开放平台版本更新,QQ 群中也会􏰁醒。在 Targets - Build Settings 中搜索 Bitcode 即可,找 到相应选项,设置为 NO。

 

四、演示:

第一种不带界面的语音听写:

DDHomeSearchViewController.m

#import <UIKit/UIKit.h>
#import "iflyMSC/iflyMSC.h"
#import "iflyMSC/IFlySpeechRecognizerDelegate.h"
#import "iflyMSC/IFlySpeechRecognizer.h"

@interface DDHomeSearchViewController : UIViewController<IFlySpeechRecognizerDelegate>
@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;   //不带界面的识别对象
@end

//初始化Appid
-(void)initAppId
{
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"xxxxxxx"];
    [IFlySpeechUtility createUtility:initString];
}

//初始化识别器
-(void)initRecognizer
{
    //单例模式,无UI的实例
    if (_iFlySpeechRecognizer == nil) {
    _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
    }

    // 设置参数
    if (_iFlySpeechRecognizer != nil) {
    //扩展参数
    [_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
    //设置听写模式
    [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    //设置最长录音时间
    [_iFlySpeechRecognizer setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
    //设置后端点
    [_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]];
    //设置前端点
    [_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]];
    //网络等待时间
    [_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
    //设置采样率,推荐使用16K
    [_iFlySpeechRecognizer setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
    //设置语言
    [_iFlySpeechRecognizer setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];
    //设置方言
    [_iFlySpeechRecognizer setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];
    //设置是否返回标点符号
    [_iFlySpeechRecognizer setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
    //设置数据返回格式
    [_iFlySpeechRecognizer setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];

    }

    // 设置代理
    _iFlySpeechRecognizer.delegate = self;
}


//实现代理方法
// 出现错误
- (void) onError:(IFlySpeechError *) error
{
    NSLog(@"出现错误 :%@",error);
}

// 识别结果 
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast
{
    NSMutableString *result = [[NSMutableString alloc] init];
    NSDictionary *dic = [results objectAtIndex:0];
    for (NSString *key in dic) {
        [result appendFormat:@"\n\n------ %@ (置信度:%@) ------ \n\n",key,[dic objectForKey:key]];
    }

    // 忽略结束。号
    if ([result hasPrefix:@"\n\n------ 。"]) {
        return;
    }

    NSLog(@"%@",result);
}


//点击开始和结束
- (IBAction)understand:(id)sender
{   
    // 开始识别
    [_iFlySpeechRecognizer startListening];
}

- (IBAction)finish:(id)sender
{   
    // 停止识别
    [_iFlySpeechRecognizer stopListening];
}


//初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAppId];
    [self initRecognizer];
}

演示结果:

2015-12-31 01:09:15.687 fa[23747:446296] [IFLYTEK] -[IFlySpeechUtility login],login=0
2015-12-31 01:09:15.720 fa[23747:446296] [IFLYTEK] +[IFlySystemInfo getWorkPath],_gMscLogPath=(null)
2015-12-31 01:09:15.723 fa[23747:446296] [IFLYTEK] login -[IFlyLoginParam toString:] SessionParam:timeout=20000,appid=567f981a,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,work_dir=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Data/Application/4C7C4E0A-9A1A-4063-8068-B8978BC7D946/Library/Caches,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi
2015-12-31 01:09:15.723 fa[23747:446296] [IFLYTEK] paramString:
timeout=20000,appid=567f981a,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,work_dir=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Data/Application/4C7C4E0A-9A1A-4063-8068-B8978BC7D946/Library/Caches,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi
2015-12-31 01:09:15.726 fa[23747:446296] [IFLYTEK] login in
2015-12-31 01:09:15.726 fa[23747:446296] [IFLYTEK] +[IFlySpeechRecognizer sharedInstance][IN],iflyInstance=0
2015-12-31 01:09:15.726 fa[23747:446296] [IFLYTEK] +[IFlySpeechRecognizer sharedInstance][OUT],iflyInstance=53658592
2015-12-31 01:09:20.069 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening][IN]
2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] isr SessionParam:engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi
2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] params=engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi
2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] grammarid=(null)
2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] audioSource=1
2015-12-31 01:09:20.070 fa[23747:446296] [IFLYTEK] isListening:NO
2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening:grammar:][IN]
2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] _mscer count0=1
2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] iflySpeechQueue0=1
2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] _mscer count2=3
2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] _mscer count3=2
2015-12-31 01:09:20.071 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer main][IN],self=53528272
2015-12-31 01:09:20.071 fa[23747:446296] [IFLYTEK] iflySpeechQueue1=3
2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening:grammar:][OUT]
2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] params:
engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi
2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] startListening audioSource:1
2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer runService][OUT]
2015-12-31 01:09:20.072 fa[23747:446296] [IFLYTEK] -[IFlySpeechRecognizer startListening][OUT]
2015-12-31 01:09:20.072 fa[23747:446340] [IFLYTEK] Converted String : 2016/12/31 01:09:20 072
2015-12-31 01:09:20.073 fa[23747:446296] [IFLYTEK] -[IFlyMscRecognizer startRecord][IN]
2015-12-31 01:09:20.073 fa[23747:446296] [IFLYTEK] +[IFlyAudioSession initRecordingAudioSession]
2015-12-31 01:09:20.074 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder setSample:],rate=16000
2015-12-31 01:09:20.074 fa[23747:446296] [IFLYTEK] +[IFlySystemInfo getWorkPath],_gMscLogPath=(null)
2015-12-31 01:09:20.074 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder start],[IN]
2015-12-31 01:09:20.124 fa[23747:446296] [IFLYTEK] pcmRecorder|timeValue:0.049352
2015-12-31 01:09:20.124 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder start],[OUT],ret =0
2015-12-31 01:09:20.124 fa[23747:446296] [IFLYTEK] -[IFlyMscRecognizer startRecord][OUT]
2015-12-31 01:09:20.124 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer beginSession][IN],self=
2015-12-31 01:09:20.124 fa[23747:446340] [IFLYTEK] beginSession before state=1
2015-12-31 01:09:20.127 fa[23747:446340] [IFLYTEK] QISRSessionBegin:engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=0,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1109.1123,openudid=5f45f8636e008e75426c1dd4bb57c83da1a9312a,carrier=,os.system=iPhone OS,os.release=8.4,os.version=8.4,os.model=x86_64,os.resolution=750*1334,os.manufact=apple,os.jailbreak=0,app.name=noName,app.path=/Users/lecason/Library/Developer/CoreSimulator/Devices/7B272449-DB68-43E8-A196-7C7D3FAA1F1D/data/Containers/Bundle/Application/E071193E-648F-4A18-9A22-378BB22F8649/fa.app,app.pkg=fa.app,net_type=wifi
2015-12-31 01:09:20.200 fa[23747:446340] [IFLYTEK] QISRRegisterNotify=0
2015-12-31 01:09:20.201 fa[23747:446340] [IFLYTEK] beginSession after state=1,_error=0
2015-12-31 01:09:20.201 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer beginSession][OUT],self=53528272
2015-12-31 01:09:20.201 fa[23747:446340] [IFLYTEK] _startDate,50338112
2015-12-31 01:09:20.205 fa[23747:446353] [IFLYTEK] recog_status_callback type==1,status=79
2015-12-31 01:09:20.383 fa[23747:446353] [IFLYTEK] recog_status_callback type==0,status=1
2015-12-31 01:09:22.946 fa[23747:446353] [IFLYTEK] recog_result_callback resultStatus==0,result=47889584,resultLen=6
2015-12-31 01:09:22.946 fa[23747:446353] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0
2015-12-31 01:09:22.947 fa[23747:446296] 

------ 中国 (置信度:100) ------ 

2015-12-31 01:09:24.496 fa[23747:446353] [IFLYTEK] recog_status_callback type==0,status=3
2015-12-31 01:09:24.497 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop][IN]
2015-12-31 01:09:24.497 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueFlush
2015-12-31 01:09:24.497 fa[23747:446353] [IFLYTEK] recog_status_callback type==0,status=3
2015-12-31 01:09:24.497 fa[23747:446296] [IFLYTEK] stop recording success
2015-12-31 01:09:24.498 fa[23747:446353] [IFLYTEK] recog_result_callback resultStatus==5,result=50332544,resultLen=3
2015-12-31 01:09:24.498 fa[23747:446353] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0
2015-12-31 01:09:24.521 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueStop
2015-12-31 01:09:24.542 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueDispose
2015-12-31 01:09:24.542 fa[23747:446296] [IFLYTEK] -[IFlyPcmRecorder stop][OUT]
2015-12-31 01:09:24.546 fa[23747:446340] [IFLYTEK] while end,self=53528272
2015-12-31 01:09:24.546 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][IN]
2015-12-31 01:09:24.547 fa[23747:446340] [IFLYTEK] info:{"rec_ustop":"0","app_fau":310,"app_start":"2016/12/31 01:09:20 072","app_frs":2875,"rec_close":4425,"app_lrs":4426,"rec_start":307,"rec_open":1,"app_ssb":52,"rec_ready":52,"app_lau":4425}
2015-12-31 01:09:24.547 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][OUT]
2015-12-31 01:09:24.548 fa[23747:446340] [IFLYTEK] -[ISREngine stopEngine] _szSession=53715544[IN]
2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd IN]
2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlySpeechRecognizer onError:] [IN] errorCode=0
2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlySpeechRecognizer onError:][OUT]
2015-12-31 01:09:24.549 fa[23747:446296] 出现错误 :<IFlySpeechError: 0x7fad02f74d80>
2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd OUT]
2015-12-31 01:09:24.549 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer main][OUT],self=53528272
2015-12-31 01:09:24.550 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer dealloc][IN],self=53528272
2015-12-31 01:09:24.550 fa[23747:446340] [IFLYTEK] -[IFlyMscRecognizer dealloc][OUT],self=53528272

第二种带界面的语音听写:

DDHomeSearchViewController.m

//
//  DDHomeSearchViewController.m//
//  Created by 夏远全 on 16/12/1.
//  Copyright © 2016年 . All rights reserved.
//

#import "DDHomeSearchController.h"

@interface DDHomeSearchViewController ()<UISearchBarDelegate,IFlyRecognizerViewDelegate>
{
    IFlyRecognizerView      *_iflyRecognizerView;
}
@property (nonatomic, strong) UISearchBar * searchBar;
@end

@implementation DDHomeSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    //初始化导航栏
    [self _createNavigationBar];
    
    //初始化语音识别
    [self initIFly];
    [self initAppid];
}


//创建导航栏
- (void)_createNavigationBar {
    
    //取消
    self.navigationItem.leftBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) ImageName:@"back" Title:@"取消" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(back)];
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    
    //搜索栏
    _searchBar.placeholder = @"请输入要搜索的商家、品类";
    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    _searchBar.autoresizesSubviews = YES;
    _searchBar.delegate = self;
    self.navigationItem.titleView = _searchBar;
    
    //搜索
    self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"语音" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceStart)];
}

//返回
-(void)back{
    [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}


//初始化Appid
-(void)initAppid{
    
    //注册讯飞语音
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"xxxxxxx"];
    [IFlySpeechUtility createUtility:initString];
}


//语音识别初始化
-(void)initIFly{
    
    //单例模式,无UI的实例
    if (_iflyRecognizerView == nil) {
        _iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
        _iflyRecognizerView.delegate = self;
    }
    
    // 设置参数
    if (_iflyRecognizerView != nil) {
        //扩展参数
        [_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
        //设置听写模式
        [_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
        //设置最长录音时间
        [_iflyRecognizerView setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
        //设置后端点
        [_iflyRecognizerView setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]];
        //设置前端点
        [_iflyRecognizerView setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]];
        //网络等待时间
        [_iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
        //设置采样率,推荐使用16K
        [_iflyRecognizerView setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
        //设置语言
        [_iflyRecognizerView setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];
        //设置方言
        [_iflyRecognizerView setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];
        //设置是否返回标点符号
        [_iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
        //设置数据返回格式
        [_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
    }
}

//语音搜索启动
-(void)voiceStart{
    //按钮变换
    self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"结束" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceEnd)];
    
    //启动识别服务
    [_iflyRecognizerView start];
}

//结束录音
-(void)voiceEnd{
    
    //按钮变换
    self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"语音" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceStart)];
    
    //结束识别服务
    [_iflyRecognizerView cancel];
}

/*识别结果返回代理
 @param resultArray 识别结果
 @ param isLast 表示是否最后一次结果
 */
- (void)onResult: (NSArray *)resultArray isLast:(BOOL) isLast
{
    [resultArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DDLog(@"%@",obj);
    }];
}
/*识别会话错误返回代理
 @ param  error 错误码
 */
- (void)onError: (IFlySpeechError *) error
{
    DDLog(@"语音识别出错:%@",error);
}

#pragma mark - UISearchBarDelegate
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [searchBar becomeFirstResponder];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    DDLog(@"%@",searchText);
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [searchBar setText:nil];
}
@end

演示结果:

2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] startListening audioSource:1
2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlySpeechRecognizer runService][OUT]
2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlySpeechRecognizer startListening][OUT]
2016-12-02 08:56:49.444 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp start][OUT]
2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView start],identifyingVIew start
2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView start][OUT]
2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyMscRecognizer startRecord][IN]
2016-12-02 08:56:49.445 BiaoJiePay[1057:379880] [IFLYTEK] +[IFlyAudioSession initRecordingAudioSession]
2016-12-02 08:56:49.449 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder setSample:],rate=16000
2016-12-02 08:56:49.449 BiaoJiePay[1057:379880] [IFLYTEK] +[IFlySystemInfo getWorkPath],_gMscLogPath=(null)
2016-12-02 08:56:49.449 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder start],[IN]
2016-12-02 08:56:49.636 BiaoJiePay[1057:379880] [IFLYTEK] pcmRecorder|timeValue:0.187140
2016-12-02 08:56:49.641 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder start],[OUT],ret =0
2016-12-02 08:56:49.641 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyMscRecognizer startRecord][OUT]
2016-12-02 08:56:49.641 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer beginSession][IN],self=
2016-12-02 08:56:49.641 BiaoJiePay[1057:379975] [IFLYTEK] beginSession before state=1
2016-12-02 08:56:49.642 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onBeginOfSpeech][IN]
2016-12-02 08:56:49.642 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onBeginOfSpeech][OUT]
2016-12-02 08:56:49.654 BiaoJiePay[1057:379975] [IFLYTEK] QISRSessionBegin:engine_type=cloud,sub=iat,language=zh_cn,plr=0,audio_source=1,vad_speech_tail=1800,msc.skin=default,rse=utf8,asr_audio_path=record.pcm,sample_rate=16000,timeout=20000,domain=iat,speech_timeout=30000,vad_timeout=1800,vad_bos=1800,rst=plain,vad_eos=1800,auf=audio/L16;rate=16000,ptt=1,voice_change=0,sch=0,accent=mandarin,msc.ver=1.0.1115.1145,xxxxxx,xxxxxxxxx........
2016-12-02 08:56:49.658 BiaoJiePay[1057:379975] [IFLYTEK] QISRRegisterNotify=0
2016-12-02 08:56:49.659 BiaoJiePay[1057:379975] [IFLYTEK] beginSession after state=1,_error=0
2016-12-02 08:56:49.659 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer beginSession][OUT],self=221654928
2016-12-02 08:56:49.659 BiaoJiePay[1057:379975] [IFLYTEK] _startDate,156534080
2016-12-02 08:56:49.790 BiaoJiePay[1057:380225] [IFLYTEK] recog_status_callback type==0,status=1
2016-12-02 08:56:50.823 BiaoJiePay[1057:380225] [IFLYTEK] recog_status_callback type==1,status=1175
2016-12-02 08:56:51.850 BiaoJiePay[1057:380225] [IFLYTEK] recog_result_callback resultStatus==0,result=呵呵,resultLen=6
2016-12-02 08:56:51.850 BiaoJiePay[1057:380225] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0
2016-12-02 08:56:51.850 BiaoJiePay[1057:380225] [IFLYTEK] -[ISRDataHander dataHander:parse:],resultString=呵呵
2016-12-02 08:56:51.850 BiaoJiePay[1057:379880] {
    "\U5475\U5475" = 100;
}
2016-12-02 08:56:52.326 BiaoJiePay[1057:380225] [IFLYTEK] recog_status_callback type==0,status=3
2016-12-02 08:56:52.326 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop][IN]
2016-12-02 08:56:52.327 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueFlush
2016-12-02 08:56:52.330 BiaoJiePay[1057:379880] [IFLYTEK] stop recording success
2016-12-02 08:56:52.346 BiaoJiePay[1057:380225] [IFLYTEK] recog_result_callback resultStatus==5,result=!,resultLen=3
2016-12-02 08:56:52.346 BiaoJiePay[1057:380225] [IFLYTEK] -[ISREngine isVoiceChange],voiceChange=0
2016-12-02 08:56:52.346 BiaoJiePay[1057:380225] [IFLYTEK] -[ISRDataHander dataHander:parse:],resultString=2016-12-02 08:56:52.351 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueStop
2016-12-02 08:56:52.373 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop]|AudioQueueDispose
2016-12-02 08:56:52.373 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyPcmRecorder stop][OUT]
2016-12-02 08:56:52.373 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onEndOfSpeech][IN]
2016-12-02 08:56:52.374 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onEndOfSpeech][OUT]
2016-12-02 08:56:52.374 BiaoJiePay[1057:379880] {
    "\Uff01" = 100;
}
2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] while end,self=221654928
2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][IN]
2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] info:{"rec_close":2883,"app_ssb":198,"app_fau":346,"rec_ready":198,"ui_lrs":2931,"app_start":"2016/12/02 08:56:49 443","app_frs":2407,"app_lau":2883,"rec_ustop":"0","rec_start":343,"app_lrs":2903,"ui_frs":2407,"rec_open":2}
2016-12-02 08:56:52.380 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setSessionInfo][OUT]
2016-12-02 08:56:52.381 BiaoJiePay[1057:379975] [IFLYTEK] -[ISREngine stopEngine] _szSession=221656648[IN]
2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd IN]
2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlySpeechRecognizer onError:] [IN] errorCode=0
2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlySpeechRecognizer onError:][OUT]
2016-12-02 08:56:52.383 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onError:][IN],errorCode=0
2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer setState:][ISREnd OUT]
2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer main][OUT],self=221654928
2016-12-02 08:56:52.383 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer dealloc][IN],self=221654928
2016-12-02 08:56:52.384 BiaoJiePay[1057:379975] [IFLYTEK] -[IFlyMscRecognizer dealloc][OUT],self=221654928
2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView onError:][IN],errorCode=0
2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] 语音识别出错:<IFlySpeechError: 0x10d39b2f0>
2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerView onError:][OUT]
2016-12-02 08:56:52.385 BiaoJiePay[1057:379880] [IFLYTEK] -[IFlyRecognizerViewImp onError:][OUT]

效果截图:(说一句“附近的商店”)

 

参考:http://lecason.com/2015/12/21/iOS-Xunfei/

posted @ 2016-12-02 09:04  XYQ全哥  阅读(9263)  评论(0编辑  收藏  举报