iOS開發(fā) 基于系統(tǒng)原生的語(yǔ)音識(shí)別助手

語(yǔ)音識(shí)別系統(tǒng)是基于系統(tǒng)的speech.framework來(lái)實(shí)現(xiàn)的

寫的demo已經(jīng)上傳github:

git地址->

1. 首先,獲取授權(quán)

//  Privacy - Speech Recognition Usage Description      錄音權(quán)限
//  Privacy - Microphone Usage Description              話筒權(quán)限

2. 基本類

@property (strong, nonatomic)SFSpeechRecognitionTask *recognitionTask; //語(yǔ)音識(shí)別任務(wù)
@property (strong, nonatomic)SFSpeechRecognizer *speechRecognizer; //語(yǔ)音識(shí)別器
@property (strong, nonatomic)SFSpeechAudioBufferRecognitionRequest *recognitionRequest; //識(shí)別請(qǐng)求
@property (strong, nonatomic)AVAudioEngine *audioEngine; //錄音引擎

3. 開啟識(shí)別任務(wù)

 self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        bool isFinal = false;
        if (result) {
            NSString * bestString = [[result bestTranscription] formattedString];
            isFinal = [result isFinal];
            if(self.sentenceSpeechHandler){
                if(self.lastString && self.lastString.length>0){
                    //獲取最后一句話
                    NSRange range = [bestString rangeOfString:self.lastString];
                    NSString * nowString = [bestString substringFromIndex:range.length];
                    self.sentenceSpeechHandler(self, nowString);
                    NSLog(@"當(dāng)前識(shí)別內(nèi)容是: %@",nowString);
                }else{
                    self.sentenceSpeechHandler(self, bestString);
                }
            }
            if (self.allSpeechHandler) {
                self.allSpeechHandler(self, [bestString copy]);
            }
            self.lastString = bestString;
            NSLog(@"進(jìn)行了一次語(yǔ)音識(shí)別,內(nèi)容是: %@",bestString);
        }
        if (error || isFinal) {
            [self.audioEngine stop];
            [inputNode removeTapOnBus:0];
            self.recognitionRequest = nil;
            self.recognitionTask = nil;
            //self.siriBtu.enabled = true;
        }
    }];

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容