1

我已經成功地創建了一個語音應用程序,它能夠基於識別關鍵字打開活動。目前,我要開始Speech to text功能,我需要點擊一個按鈕。我現在不想手動按下按鈕,並希望使用連續偵聽器來觸發按鈕。Pocketsphinx只是聽一個命令

我一直在研究使用pocketsphinx,並且在聽到這個關鍵字時添加了一個關鍵字「listen to command」我想讓按鈕被自動按下,然後我可以在代碼中添加一系列命令。我不需要脫機語音文本等,因此我使用谷歌的語音文本選項,但我打算使用pocketsphinx觸發谷歌的語音文本功能。

下面是大多數,我部分來自pocketsphinx修改代碼:

public class PocketSphinxActivity extends Activity implements RecognitionListener { 

/* Named searches allow to quickly reconfigure the decoder */ 
private static final String KWS_SEARCH = "wakeup"; 
private static final String FORECAST_SEARCH = "forecast"; 
private static final String DIGITS_SEARCH = "digits"; 
private static final String PHONE_SEARCH = "phones"; 
private static final String MENU_SEARCH = "menu"; 

/* Keyword we are looking for to activate menu */ 
private static final String KEYPHRASE = "listen to command"; //adjust this keyphrase! 

private SpeechRecognizer recognizer; 
private HashMap < String, Integer > captions; 

@Override 
public void onCreate(Bundle state) { 
    super.onCreate(state); 

    // Prepare the data for UI 
    captions = new HashMap < String, Integer >(); 
    captions.put(KWS_SEARCH, R.string.kws_caption); 
    captions.put(MENU_SEARCH, R.string.menu_caption); 
    captions.put(DIGITS_SEARCH, R.string.digits_caption); 
    captions.put(PHONE_SEARCH, R.string.phone_caption); 
    captions.put(FORECAST_SEARCH, R.string.forecast_caption); 
    setContentView(R.layout.main); 
    ((TextView) findViewById(R.id.caption_text)) 
    .setText("Preparing the recognizer"); 

    // Recognizer initialization is a time-consuming and it involves IO, 
    // so we execute it in async task 

    new AsyncTask < Void, Void, Exception >() { 
    @Override 
    protected Exception doInBackground(Void...params) { 
    try { 
    Assets assets = new Assets(PocketSphinxActivity.this); 
    File assetDir = assets.syncAssets(); 
    setupRecognizer(assetDir); 
    } catch (IOException e) { 
    return e; 
    } 
    return null; 
    } 

    @Override 
    protected void onPostExecute(Exception result) { 
    if (result != null) { 
    ((TextView) findViewById(R.id.caption_text)) 
    .setText("Failed to init recognizer " + result); 
    } else { 
    switchSearch(KWS_SEARCH); 
    } 
    } 
    }.execute(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    recognizer.cancel(); 
    recognizer.shutdown(); 
} 

/** 
    * In partial result we get quick updates about current hypothesis. In 
    * keyword spotting mode we can react here, in other modes we need to wait 
    * for final result in onResult. 
    */ 
@Override 
public void onPartialResult(Hypothesis hypothesis) { 
    if (hypothesis == null) 
    return; 

    String text = hypothesis.getHypstr(); 
    if (text.equals(KEYPHRASE)) 
    switchSearch(MENU_SEARCH); 
    else if (text.equals(DIGITS_SEARCH)) 
    switchSearch(DIGITS_SEARCH); 
    else if (text.equals(PHONE_SEARCH)) 
    switchSearch(PHONE_SEARCH); 
    else if (text.equals(FORECAST_SEARCH)) 
    switchSearch(FORECAST_SEARCH); 
    else 
    ((TextView) findViewById(R.id.result_text)).setText(text); 
} 

/** 
    * This callback is called when we stop the recognizer. 
    */ 
@Override 
public void onResult(Hypothesis hypothesis) { 
    ((TextView) findViewById(R.id.result_text)).setText(""); 
    if (hypothesis != null) { 
    String text = hypothesis.getHypstr(); 
    makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
public void onBeginningOfSpeech() {} 

/** 
    * We stop recognizer here to get a final result 
    */ 
@Override 
public void onEndOfSpeech() { 
    if (!recognizer.getSearchName().equals(KWS_SEARCH)) 
    switchSearch(KWS_SEARCH); 
} 

private void switchSearch(String searchName) { 
    recognizer.stop(); 

    // If we are not spotting, start listening with timeout (10000 ms or 10 seconds). 
    if (searchName.equals(KWS_SEARCH)) 
    recognizer.startListening(searchName); 
    else 
    recognizer.startListening(searchName, 10000); 

    String caption = getResources().getString(captions.get(searchName)); 
    ((TextView) findViewById(R.id.caption_text)).setText(caption); 
} 

private void setupRecognizer(File assetsDir) throws IOException { 
    // The recognizer can be configured to perform multiple searches 
    // of different kind and switch between them 

    recognizer = defaultSetup() 
    .setAcousticModel(new File(assetsDir, "en-us-ptm")) 
    .setDictionary(new File(assetsDir, "cmudict-en-us.dict")) 

    // To disable logging of raw audio comment out this call (takes a lot of space on the device) 
    .setRawLogDir(assetsDir) 

    // Threshold to tune for keyphrase to balance between false alarms and misses 
    .setKeywordThreshold(1e-45 f) 

    // Use context-independent phonetic search, context-dependent is too slow for mobile 
    .setBoolean("-allphone_ci", true) 

    .getRecognizer(); 
    recognizer.addListener(this); 

    /** In your application you might not need to add all those searches. 
    * They are added here for demonstration. You can leave just one. 
    */ 

    // Create keyword-activation search. 
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE); 

    // Create grammar-based search for selection between demos 
    File menuGrammar = new File(assetsDir, "menu.gram"); 
    recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar); 

    // Create grammar-based search for digit recognition 
    File digitsGrammar = new File(assetsDir, "digits.gram"); 
    recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar); 

    // Create language model search 
    File languageModel = new File(assetsDir, "weather.dmp"); 
    recognizer.addNgramSearch(FORECAST_SEARCH, languageModel); 

    // Phonetic search 
    File phoneticModel = new File(assetsDir, "en-phone.dmp"); 
    recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel); 
} 

@Override 
public void onError(Exception error) { 
    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage()); 
} 

@Override 
public void onTimeout() { 
    switchSearch(KWS_SEARCH); 
} 

我不是最感興趣的命令,我只是單純的想,因爲它是打開的應用程序,儘快聽(這它目前那樣),只要一個人說Listen to command它,然後按下一個我已經設置的bVoice

一個id如果有人能好心幫我修改上面的代碼,讓我知道刪除什麼,什麼添加一個按鈕這將不勝感激。另外請注意,如果有更簡單的方法來做到這一點,請隨時分享(不需要使用自定義關鍵詞)。

回答

0

這樣的事情,而你並不需要「按下按鈕」,可以直接調用按鈕的回調,而不是和執行你感興趣的實際步驟。

public class PocketSphinxActivity extends Activity implements RecognitionListener { 

private static final String KWS_SEARCH = "wakeup"; 
private static final String KEYPHRASE = "listen to command"; //adjust this keyphrase! 

private SpeechRecognizer recognizer; 

@Override 
public void onCreate(Bundle state) { 
    super.onCreate(state); 

    setContentView(R.layout.main); 
    ((TextView) findViewById(R.id.caption_text)) 
    .setText("Preparing the recognizer"); 

    new AsyncTask < Void, Void, Exception >() { 
    @Override 
    protected Exception doInBackground(Void...params) { 
    try { 
    Assets assets = new Assets(PocketSphinxActivity.this); 
    File assetDir = assets.syncAssets(); 
    setupRecognizer(assetDir); 
    } catch (IOException e) { 
    return e; 
    } 
    return null; 
    } 

    @Override 
    protected void onPostExecute(Exception result) { 
    if (result != null) { 
    ((TextView) findViewById(R.id.caption_text)) 
    .setText("Failed to init recognizer " + result); 
    } else { 
    recognizer.startListening(KWS_SEARCH); 
    } 
    } 
    }.execute(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    recognizer.cancel(); 
    recognizer.shutdown(); 
} 

/** 
    * In partial result we get quick updates about current hypothesis. In 
    * keyword spotting mode we can react here, in other modes we need to wait 
    * for final result in onResult. 
    */ 
@Override 
public void onPartialResult(Hypothesis hypothesis) { 
    if (hypothesis == null) 
    return; 

    String text = hypothesis.getHypstr(); 
    if (text.equals(KEYPHRASE)) { 
     recognizer.cancel(); 
     performAction();  // <- You have to implement this 
     recognizer.startListening(KWS_SEARCH); 
    } 
} 

@Override 
public void onResult(Hypothesis hypothesis) {} 

@Override 
public void onBeginningOfSpeech() {} 

@Override 
public void onEndOfSpeech() {} 

@Override 
public void onTimeout() {} 

private void setupRecognizer(File assetsDir) throws IOException { 
    // The recognizer can be configured to perform multiple searches 
    // of different kind and switch between them 

    recognizer = defaultSetup() 
    .setAcousticModel(new File(assetsDir, "en-us-ptm")) 
    .setDictionary(new File(assetsDir, "cmudict-en-us.dict")) 
    .getRecognizer(); 
    recognizer.addListener(this); 

    // Create keyword-activation search. 
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE); 
} 

@Override 
public void onError(Exception error) { 
    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage()); 
} 

public void peformAction() { 
    // do here whatever you want 
} 
+0

感謝,當你寫「 performAction()「我是否需要用諸如」performClick「之類的東西替換它?如:Handler handler = new Handler(); (); handler.postDelayed(new Runnable(){ @Override public void run(){ c.performClick(); } },500); //在500ms後自動點擊'Blogin'按鈕 –

+0

'performAction'只是一個通用名稱,如果你更喜歡這個名字,你可以用'performClick'替換它。 –

+0

你不需要一個處理程序,你可以直接運行它 –