2011-01-19 34 views
2

是否有任何我可以在離線模式下使用Android的語音到文本功能。在離線模式下Android上的語音到文字

在給定示例VoiceRecognition.java中,它啓動並使用intent RecognizerIntent.ACTION_RECOGNIZE_SPEECH進行活動。

這是否意味着需要安裝任何其他apk才能正常工作,或者是否需要編寫自己的應用程序才能啓動此目標。

我一直在尋找這很長的時間,但感到困惑......

這裏是我使用的代碼..

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 

private ListView mList; 

/** 
* Called with the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Inflate our UI from its XML layout description. 
    setContentView(R.layout.voice_recognition); 

    // Get display items for later interaction 
    Button speakButton = (Button) findViewById(R.id.btn_speak); 

    mList = (ListView) findViewById(R.id.list); 

    // Check to see if a recognition activity is present 
    PackageManager pm = getPackageManager(); 
    List<ResolveInfo> activities = pm.queryIntentActivities(
      new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
    if (activities.size() != 0) { 
     speakButton.setOnClickListener(this); 
    } else { 
     speakButton.setEnabled(false); 
     speakButton.setText("Recognizer not present"); 
    } 
} 

/** 
* Handle the click on the start recognition button. 
*/ 
public void onClick(View v) { 
    if (v.getId() == R.id.btn_speak) { 
     startVoiceRecognitionActivity(); 
    } 
} 

/** 
* Fire an intent to start the speech recognition activity. 
*/ 
private void startVoiceRecognitionActivity() { 
    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"); 
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
} 

/** 
* Handle the results from the recognition activity. 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { 
     // Fill the list view with the strings the recognizer thought it could have heard 
     ArrayList<String> matches = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS); 
     mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
       matches)); 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 

在運行這段代碼它給識別器沒有出現,這意味着不存在這樣的活動。如何解決這個問題?

+0

使用哪種設備測試代碼?你正在使用哪個Android版本? – Janusz 2011-01-19 10:44:28

+0

我試圖在模擬器上找出它!當我把它放在Glalaxy Tab上時,它的工作原理......雖然它能夠識別句子,如果我們慢慢說話......任何想法都可以得到更好的認可! – amiekuser 2011-01-28 08:21:17

回答

6

我想你有兩個問題。首先,是的識別器功能在所有設備上都不可用。確保您安裝並更新最新的Android版Google語音搜索。我相信它會安裝最新的識別器。請參閱http://www.google.com/mobile/voice-actions/這可能會有幫助。

正如Dante Jiang在Converting speech to text中所說,根據this articleGoogle Voice Search就是你實際需要的。

Android的SDK可以很容易地 直接集成語音輸入到 自己的應用程序,只需複製並此示例應用程序,以 貼上手。 Android是一個開放的 平臺,因此您的應用程序可以使用 潛在地利用設備 上的任何語音 識別服務,該服務已註冊接收 RecognizerIntent。谷歌的語音 搜索應用程序,這是 預裝在許多Android設備, 由 顯示「請開始說話」對話框,並 音頻流,谷歌的 服務器,在使用相同的服務器當 用戶點擊的響應一個RecognizerIntent麥克風按鈕上的 搜索小部件或支持語音的 鍵盤。 (您可以檢查語音 搜索安裝在設置➝ 應用➝管理應用程序。)

在代碼中,你應該檢查,看看是否表彰活動是存在的。我有我用過的以下代碼片段:

// Check to see if a recognition activity is present 
PackageManager pm = getPackageManager(); 
List<ResolveInfo> activities = pm.queryIntentActivities(
     new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
if (activities.size() != 0) 
{ 
    speakButton.setOnClickListener(this); 
} 
else 
{ 
    speakButton.setEnabled(false); 
    speakButton.setText(R.string.recognizer_not_present); 
} 

第二個問題是Android語音識別需要Internet連接。識別不在設備上執行,而是使用Google網絡服務。所以,你必須在線。有關Web服務的一些信息可在http://waxy.org/2008/11/deconstructing_google_mobiles_voice_search_on_the_iphone/獲得。