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/

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/6124418.html ,如需转载请自行联系原作者
相关实践学习
达摩院智能语音交互 - 声纹识别技术
声纹识别是基于每个发音人的发音器官构造不同,识别当前发音人的身份。按照任务具体分为两种: 声纹辨认:从说话人集合中判别出测试语音所属的说话人,为多选一的问题 声纹确认:判断测试语音是否由目标说话人所说,是二选一的问题(是或者不是) 按照应用具体分为两种: 文本相关:要求使用者重复指定的话语,通常包含与训练信息相同的文本(精度较高,适合当前应用模式) 文本无关:对使用者发音内容和语言没有要求,受信道环境影响比较大,精度不高 本课程主要介绍声纹识别的原型技术、系统架构及应用案例等。 讲师介绍: 郑斯奇,达摩院算法专家,毕业于美国哈佛大学,研究方向包括声纹识别、性别、年龄、语种识别等。致力于推动端侧声纹与个性化技术的研究和大规模应用。
相关文章
|
4月前
|
语音技术 开发工具 图形学
Unity与IOS⭐一、百度语音IOS版Demo调试方法
Unity与IOS⭐一、百度语音IOS版Demo调试方法
|
4月前
|
语音技术 开发工具 图形学
Unity与IOS⭐二、Unity接入IOS版百度语音:语音唤醒
Unity与IOS⭐二、Unity接入IOS版百度语音:语音唤醒
Unity与IOS⭐二、Unity接入IOS版百度语音:语音唤醒
|
机器学习/深度学习 缓存 自然语言处理
谷歌Recorder实现说话人自动标注,功能性与iOS语音备忘录再度拉大
谷歌Recorder实现说话人自动标注,功能性与iOS语音备忘录再度拉大
141 0
|
人工智能 算法 API
iOS MachineLearning 系列(13)—— 语音与音频相关的AI能力
在语音分析方面,iOS中提供了原生的Speech框架,这个框架可以实时的将语音解析成文本。这个能力非常强大,使用它我们可以实现类似实时翻译的功能。对于非语音的音频,也有一些原生的AI能力可以使用,例如分析语音的类型。SoundAnalysis框架能够识别300多种声音,我们也可以使用自己训练的模型来处理定制化的音频识别需求。
364 0
|
消息中间件 存储 API
iOS小技能:队列管理推送通知,解决收款到账并发语音播报问题。
需求:收款到账语音提醒功能 NSE是比Voip更优雅的解决方案,完成迁移后,总体代码量也比Voip方案少了不少。
309 0
iOS小技能:队列管理推送通知,解决收款到账并发语音播报问题。
iOS15 推送动态语音播报解决方案
iOS15之后,推送多条语音会产生多条横幅,对于动态金额语音,多条横幅是不可取的
470 0
|
语音技术 开发者
iOS10语音识别框架SpeechFramework应用(一)
iOS10语音识别框架SpeechFramework应用
360 0
iOS10语音识别框架SpeechFramework应用(一)
|
语音技术 开发者
iOS10语音识别框架SpeechFramework应用(二)
iOS10语音识别框架SpeechFramework应用
630 0
|
安全 Java 关系型数据库
产品百科 |零门槛搭建一个 1 对 1 语音聊天 Demo (iOS 版)
您可以阅读本文,快速了解 1 对 1 语音聊天的基本概念和实现方法。
产品百科 |零门槛搭建一个 1 对 1 语音聊天 Demo (iOS 版)