簡體   English   中英

識別偵聽器Android Studio,它不起作用

[英]Recognition listener android studio, it doesn't work

我從互聯網上復制了此文件,其他人則說是可行的,但是當我嘗試講話時卻無濟於事。

我試圖在onBeginingOfSpeech()方法之外編寫有關意圖的代碼,並且它可以工作。

public class Schermata extends ActionBarActivity implements RecognitionListener{

    SpeechRecognizer speechRecognizer;
    TextView test;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schermata);
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        test = (TextView) findViewById(R.id.test);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_schermata, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        test.setText("INIZIO");

    }

    @Override
    public void onBeginningOfSpeech() {
        test.setText("INIZIO");
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechRecognizer.startListening(intent);

    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onEndOfSpeech() {
        test.setText("FINE");
    }

    @Override
    public void onError(int error) {

    }

    @Override
    public void onResults(Bundle results) {
        ArrayList<String> res = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        String s = "";
        for (String result : res)
            s += result + "\n";
        test.setText(s);
    }

    @Override
    public void onPartialResults(Bundle partialResults) {

    }

    @Override
    public void onEvent(int eventType, Bundle params) {

    }
}

如果我理解正確,並且在inBeginningOfSpeech()之外定義了Intent時這對您有用,那么只需將其放在它的外面,將它放在那里是錯誤的,我將進行解釋。

函數onBeginningOfSpeech()(也:onResults,onReadyForSpeech等)在識別器開始工作后即被調用,並且僅是識別器聽到某人講話的事件的回調,因此您需要准備好該意圖並在調用此功能之前與識別器配對。

嘗試創建一個“ initialize()”函數,該函數會在調用之前設置所有這些函數

speechRecognizer.startListening(intent);

換句話說,使用this,而不是您在'onBeginningOfSpeech()'上的內容

 public void initialize(){
    Intent intent = new   Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    speechRecognizer.startListening(intent);
}

否則,您將以錯誤的方式使用onResults(),則應僅提取提供給您的ArrayList的第一個String,而其他字符串只是同一語音的最差結果。

<uses-permission android:name="android.permission.RECORD_AUDIO" />

請確保您在清單中具有RECORD_AUDIO權限,否則,語音識別將無法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM