有關整合科大訊飛sdk的語音(一)帶語音的介面
最近專案需要整合語音這塊,我選擇了科大訊飛的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{
[
}
[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);
}
}