語(yǔ)音識(shí)別:
- 創(chuàng)建應(yīng)用,獲取APPID
- 添加依賴庫(kù),還有content.framework
- bitcode的設(shè)置
- 引入頭文件,遵循代理IFlySpeechRecognizerDelegate。
- 代碼實(shí)現(xiàn)
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",IFlyAppID];
[IFlySpeechUtility createUtility:initString];
-(void)initRecognizer{
//單例模式,無(wú)UI的實(shí)例
if (_iFlySpeechRecognizer == nil) {
_iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
[_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
//設(shè)置聽(tīng)寫(xiě)模式
[_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
}
_iFlySpeechRecognizer.delegate = self;
if (_iFlySpeechRecognizer != nil) {
IATConfig *instance = [IATConfig sharedInstance];
//設(shè)置最長(zhǎng)錄音時(shí)間
[_iFlySpeechRecognizer setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
//設(shè)置后端點(diǎn)
[_iFlySpeechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];
//設(shè)置前端點(diǎn)
[_iFlySpeechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
//設(shè)置聽(tīng)寫(xiě)模式
[_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
_iFlySpeechRecognizer.delegate = self;
//網(wǎng)絡(luò)等待時(shí)間
[_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
//設(shè)置采樣率,推薦使用16K
[_iFlySpeechRecognizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];
if ([instance.language isEqualToString:[IATConfig chinese]]) {
//設(shè)置語(yǔ)言
[_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
//設(shè)置方言
[_iFlySpeechRecognizer setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
}else if ([instance.language isEqualToString:[IATConfig english]]) {
[_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
}
//設(shè)置是否返回標(biāo)點(diǎn)符號(hào)
[_iFlySpeechRecognizer setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];
}
}
-(void)onResults:(NSArray *)results isLast:(BOOL)isLast
{
NSMutableString *result = [[NSMutableString alloc]init];
NSDictionary *dic = [results objectAtIndex:0];
for (NSString *key in dic) {
[result appendFormat:@"%@",key];
}
NSString *resu = [ISRDataHelper stringFromJson:result];
if ([resu isEqualToString:@"。"] || [resu isEqualToString:@"?"] || [resu isEqualToString:@"!"]) {
NSLog(@"末尾標(biāo)點(diǎn)符號(hào):%@",resu);
[_iFlySpeechRecognizer destroy];
}else{
NSLog(@"%@",resu);
//[self createTextAndVoiceStr:resu];
[self createTextAndVoiceStr:resu AndType:YES];
}
}
// 出現(xiàn)錯(cuò)誤
- (void) onError:(IFlySpeechError *) error
{
NSLog(@"出現(xiàn)錯(cuò)誤 :%@",error);
}
// 按下錄音按鈕開(kāi)始錄音
- (void)didStartRecordingVoiceAction:(UIView *)recordView
{
[BZGJAliAnalytics sendCustomBuilder:Ali_press_im_robot_botton];
_iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
[_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
//開(kāi)始錄音
BOOL ret = [_iFlySpeechRecognizer startListening];
if (ret) {
NSLog(@"啟動(dòng)成功");
}else{
NSLog(@"啟動(dòng)失敗");
}
if ([self canRecord]) {
DXRecordView *tmpView = (DXRecordView *)recordView;
//tmpView.center = self.view.center;
tmpView.frame = CGRectMake(mScreenWidth/2-70, mScreenHeight/2-70, 140, 140);
[self.view addSubview:tmpView];
[self.view bringSubviewToFront:recordView];
int x = arc4random() % 100000;
NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
NSString *fileName = [NSString stringWithFormat:@"%d%d",(int)time,x];
[[EMCDDeviceManager sharedInstance] asyncStartRecordingWithFileName:fileName
completion:^(NSError *error)
{
if (error) {
NSLog(NSLocalizedString(@"message.startRecordFail", @"failure to start recording"));
}
}];
}else{
CustomAlertView *alertViewMKF = [[CustomAlertView alloc]initWithTitle:@"溫馨提示!"
message:@"請(qǐng)?jiān)谠O(shè)置->隱私中設(shè)置允許使用麥克風(fēng)"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定", nil];
[alertViewMKF show];
}
}
//手指向上滑動(dòng)取消錄音
- (void)didCancelRecordingVoiceAction:(UIView *)recordView
{
//取消錄音
[_iFlySpeechRecognizer cancel];
[[EMCDDeviceManager sharedInstance] cancelCurrentRecording];
}
// 松開(kāi)手指完成錄音
- (void)didFinishRecoingVoiceAction:(UIView *)recordView
{
//停止錄音開(kāi)始識(shí)別
[_iFlySpeechRecognizer stopListening];
if ([self canRecord]){
__weak typeof(self) weakSelf = self;
[[EMCDDeviceManager sharedInstance] asyncStopRecordingWithCompletion:^(NSString *recordPath, NSInteger aDuration, NSError *error) {
if (!error) {
EMChatVoice *voice = [[EMChatVoice alloc] initWithFile:recordPath
displayName:@"audio"];
voice.duration = aDuration;
[weakSelf sendAudioMessage:voice];
}else {
[weakSelf showHudInView:self.view hint:NSLocalizedString(@"錄制時(shí)間過(guò)短", @"The recording time is too short")];
weakSelf.chatToolBar.recordButton.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf hideHud];
weakSelf.chatToolBar.recordButton.enabled = YES;
});
}
}];
}
}
//麥克風(fēng)權(quán)限
- (BOOL)canRecord
{
__block BOOL bCanRecord = YES;
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
bCanRecord = granted;
}];
}
}
return bCanRecord;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。