首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >自定义语音识别对话框

自定义语音识别对话框
EN

Stack Overflow用户
提问于 2016-05-08 01:45:44
回答 2查看 4.8K关注 0票数 7

我想知道是否有任何方法可以在我的应用程序中更改和自定义语音识别对话框的样式?

例如:更改谷歌徽标或文本。

我使用这段代码,它是完整的吗?

代码语言:javascript
复制
public void onReadyForSpeech(Bundle params) {
     proccessTXT.setText("Speak now!");
}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float rmsdB) {

}

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

}

@Override
public void onEndOfSpeech() {
    proccessTXT.setText("Waiting");
}

@Override
public void onError(int error) {
     proccessTXT.setText(R.string.toast_disconnect);
}

@Override
public void onResults(Bundle results) {
        match_text_dialog = new Dialog(MainActivity.this);
        match_text_dialog.setContentView(R.layout.dialog_maches_flag);
        match_text_dialog.setTitle(R.string.selection_list);
        textlist = (ListView) match_text_dialog.findViewById(R.id.list);
        matches_text = getIntent().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, matches_text);
        textlist.setAdapter(adapter);
        textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override

            public void onItemClick(AdapterView parent, View view,
                                    int position, long id) {

                type_texts = matches_text.get(position);

                speech_text.append(type_texts + " ");

                match_text_dialog.hide();

                //  speech_text.setCustomSelectionActionModeCallback(new SelectText());
                actionMode = MainActivity.this.startActionMode(new SelectText());
            }
        });
        match_text_dialog.show();// show dialog

}

@Override
public void onPartialResults(Bundle partialResults) {

}

这是LogCat:

代码语言:javascript
复制
FATAL EXCEPTION: main
                                                                                Process: PACKAGE, PID: 25645
                                                                                java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                                                                                    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
                                                                                    at android.widget.ListView.setAdapter(ListView.java:502)
                                                                                    at PACKAGE.MainActivity.onResults(MainActivity.java:245)
                                                                                    at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:456)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6837)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-08 01:53:33

作为一个选项,您可以使用以下命令启动语音识别器ACTION_RECOGNIZE_SPEECH(无UI)并显示所需的任何对话框

代码语言:javascript
复制
SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(this);
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
speechRecognizer.startListening(speechIntent);

在这里,您需要实现RecognitionListener

因此,您可以在public void onReadyForSpeech(Bundle params)

回调。取消public void onResults(Bundle results)

或者public void onError(int error)

票数 11
EN

Stack Overflow用户

发布于 2021-02-24 23:33:18

代码语言:javascript
复制
//use this for implementing speech to text translation in android via java
micbtn.setOnClickListener(v -> {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
    try {
        startActivityForResult(intent, 1);
        input.setText("");
    } catch (ActivityNotFoundException e) {
        Toast.makeText(getApplicationContext(), "Your Device doesn't Support this Feature", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
});

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data ) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case 1:
        if (resultCode == RESULT_OK && data != null) {
            ArrayList info = data . getStringArrayListExtra (RecognizerIntent.EXTRA_RESULTS);
            input.setText(info.get(0).replaceAll("\\s", "").toUpperCase());

        }
        break;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37091779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档