Android TTS 實(shí)戰(zhàn)之中文語音識別
不能用中文的語音識別感覺很不爽,下午終于弄出來了,網(wǎng)上查找資料說要使用開源項(xiàng)目eyes-free或者下載科大訊飛的 jar 包。但我是用谷歌的 voice search (現(xiàn)在有支持中文了)來做的。
名詞解釋
?語音搜索(voice search)是一種語音識別技術(shù),它使用戶能夠通過大聲說出條目來進(jìn)行搜索,而不用將它們輸入到搜索欄來搜索。智能手機(jī)以及其他小型的具有網(wǎng)絡(luò)功能的移動(dòng)設(shè)備的劇增激發(fā)了大家對語音搜索的興趣。
語音搜索(voice search)的應(yīng)用包括:
進(jìn)行搜索引擎查詢。明確具體的要求。請求具體信息,如股票報(bào)價(jià)。啟動(dòng)程序并選擇選項(xiàng)。尋找音頻或視頻文件里的內(nèi)容。語音撥號。
語音搜索(voice search)通常是一個(gè)應(yīng)用軟件,但它也可以是一個(gè)服務(wù)。語音搜索應(yīng)用程序,如具有語音功能的谷歌移動(dòng)和iPhone的Vlingo,依靠的是語音識別程序。
java代碼
package?com.example.speechtotextdemo;
import?java.util.ArrayList;
import?android.app.Activity;
import?android.content.ActivityNotFoundException;
import?android.content.Intent;
import?android.os.Bundle;
import?android.speech.RecognizerIntent;
import?android.view.Menu;
import?android.view.View;
import?android.widget.Button;
import?android.widget.TextView;
import?android.widget.Toast;
public?class?MainActivity?extends?Activity?{
????private?static?final?int?VOICE_RECOGNITION?=?83;????//startActivityForResult操作要求的標(biāo)識碼
????private?Button?btnSpeak;
????private?TextView?txtText;
????@Override
????public?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.fragment_main);
????????txtText?=?(TextView)?findViewById(R.id.txtText);
????????btnSpeak?=?(Button)?findViewById(R.id.btnSpeak);
????????btnSpeak.setOnClickListener(new?View.OnClickListener()?{
????????????@Override
????????????public?void?onClick(View?v)?{
????????????????Intent?intent?=?new?Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
????????????????intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
????????????????????????RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
????????????????intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
????????????????????????"Speech?recognition?demo");?????????????//設(shè)置語音識別Intent調(diào)用的特定屬性參數(shù)
????????????????try?{
????????????????????//啟動(dòng)一個(gè)要求有返回值的activity調(diào)用
????????????????????startActivityForResult(intent,?VOICE_RECOGNITION);
????????????????????txtText.setText("");
????????????????}?catch?(ActivityNotFoundException?a)?{
????????????????????Toast?t?=?Toast.makeText(getApplicationContext(),
????????????????????????????"Opps!?Your?device?doesn't?support?Speech?to?Text",
????????????????????????????Toast.LENGTH_SHORT);
????????????????????t.show();
????????????????}
????????????}
????????});
????}
????@Override
????public?boolean?onCreateOptionsMenu(Menu?menu)?{
????????getMenuInflater().inflate(R.menu.main,?menu);
????????return?true;
????}
????@Override
????protected?void?onActivityResult(int?requestCode,?int?resultCode,?Intent?data)?{
????????super.onActivityResult(requestCode,?resultCode,?data);
????????switch?(requestCode)?{
????????????case?VOICE_RECOGNITION:?{
????????????????if?(resultCode?==?RESULT_OK?&&?null?!=?data)?{
????????????????????ArrayListtext?=?data
????????????????????????????.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
????????????????????txtText.setText(text.get(0));
????????????????}
????????????????break;
????????????}
????????}
????}
}




