Android語音辨識功能使用總結

來源:互聯網
上載者:User

標籤:語音辨識   android   speechrecognizer   

Android引入了對語音辨識的支援。語音辨識主要位於android.speech包中,主要的類包括SpeechRecognizer、RecognitionService、RecognizerIntent、RecognizerResultsIntent、RecognitionListener等類和介面。

SpeechRecognizer提供了接入語音辨識服務的介面,下面是SpeechRecognizer的常用方法:  

<span style="font-size:18px;">  createSpeechRecognizer()    //建立語音辨識器        setRecognitionListener()    //設定監聽器        startListening()    //開始語音辨識        stopListening()    //結束語音辨識</span>

語音辨識要求"android.permission.RECORD_AUDIO"許可權。

為了進行語音辨識,首先需要發布一個語音辨識的Activity,然後系統會啟動語音辨識引擎,並對使用者的語音進行處理,處理的結果將會通過onActivityResult()方法被返回。下面是Android中的一個實現:

SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(getPackageName(),                BDRecognitionService.class.getName()));        sr.setRecognitionListener(new listener());        sr.startListening(new Intent(getIntent().getAction()).putExtras(getIntent()));

回調方法

<span style="text-indent: 24px; background-color: rgb(240, 240, 240);">class listener implements RecognitionListener{</span>        @Override        public void onReadyForSpeech(Bundle params) {                    }        @Override        public void onBeginningOfSpeech() {                    }        @Override        public void onRmsChanged(float rmsdB) {                    }        @Override        public void onBufferReceived(byte[] buffer) {                   }        @Override        public void onEndOfSpeech() {                }        @Override        public void onError(int error) {                                }        @Override        public void onResults(Bundle results) {                        //具體處理識別結果        }        @Override        public void onPartialResults(Bundle partialResults) {                              }        @Override        public void onEvent(int eventType, Bundle params) {                   }            }


注意:android雖然提供了該介面,但是沒有提供具體的識別功能

new ComponentName(getPackageName(),BDRecognitionService.class.getName())

因此執行個體化時的參數指定了具體實現功能的service,這就需要開發人員實現RecognitionService這個服務介面,下一篇文章將介紹如何?該類。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.