有关集成科大讯飞sdk的语音(一)带语音的界面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28551705/article/details/79171804

最近项目需要集成语音这块,我选择了科大讯飞的sdk,IFlySpeechRecognizer是不带界面的语音听写控件,显示界面的时候需要用户进行自定义语音动画界面,IFlyRecognizerView是带界面的控件.我会分别介绍这两种显示.

带语音的界面:

#import "iflyMSC/IFlyMSC.h"

@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;//带界面的识别对象

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

   

   if (_iflyRecognizerView == nil) {

        [self initIflySound];

        

    }else{

        

        [_iflyRecognizerView start];

    }

    [self.searchBar resignFirstResponder];

}


- (void)initIflySound{

    

    //UI显示剧中

    _iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center];

    

    [_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];

    

    //设置听写模式

    [_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];

    

    _iflyRecognizerView.delegate = self;

    

    //设置最长录音时间

    [_iflyRecognizerView setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];

    //设置后端点

    [_iflyRecognizerView setParameter:@"3000" forKey:[IFlySpeechConstant VAD_EOS]];

    //设置前端点

    [_iflyRecognizerView setParameter:@"3000" 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:@"0" forKey:[IFlySpeechConstant ASR_PTT]];

    //设置音频来源为麦克风

    [_iflyRecognizerView setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"];

    

    //设置听写结果格式为json

    [_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];

    

    //保存录音文件,保存在sdk工作路径中,如未设置工作路径,则默认保存在library/cache下

    [_iflyRecognizerView setParameter:@"asr.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];

    

    BOOL ret = [_iflyRecognizerView start];

    NSLog(@"%d",ret);

}


#pragma mark - IFlyRecognizerViewDelegate

- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast

{

    NSMutableString *result = [[NSMutableString alloc] init];

    NSDictionary *dic = [resultArray objectAtIndex:0];

    

    for (NSString *key in dic) {

        [result appendFormat:@"%@",key];

    }

    

    if (!isLast) {

      

        self.searchBar.text = result;

        

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

    }else{

        //每次返回的result都是你说的每一段话,当isLast=YES是,证明你已经识别完所有内容,处理逻辑

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

    }

}

如果只需要用科大讯飞自带的语音识别的UI界面,以上代码就可以集成了!
由于我的项目是用到的不带语音是识别的,所以下一篇,我会重点介绍不带语音的,里面还会加入有关麦克风权限设置的问题处理办法


猜你喜欢

转载自blog.csdn.net/qq_28551705/article/details/79171804