有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SpeechtoText:不显示Google的对话框

onCreateView()内,我正在使用谷歌的语音转换文本。我的代码工作正常,但我想避免谷歌的对话框。我知道这是可能的,因为我在StackOverlow上看到了很多问题,但我仍然不知道对话框不应该出现的确切位置

这就是我到目前为止所做的:

@SuppressLint("ValidFragment")
public class Dialog extends DialogFragment implements RecognitionListener {
    private final int _layout;
    private TextView _dialogText;

    @SuppressLint("ValidFragment")
    public Dialog(int layout) {
        _layout = layout;
    }

    @Override
    public void onReadyForSpeech(Bundle params) {

    }

    @Override
    public void onBeginningOfSpeech() {

    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

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

    }

    @Override
    public void onEndOfSpeech() {

    }

    @Override
    public void onError(int error) {

    }

    @Override
    public void onResults(Bundle results) {

    }

    @Override
    public void onPartialResults(Bundle partialResults) {

    }

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

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (_layout == R.layout.fragment_dialog) {
            if (requestCode == 10) { // ResultCode matches with RequestCode (see startActivityForResult())
                if (resultCode == RESULT_OK && data != null) {
                    ArrayList<String> sstResult = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    _dialogText.setText(sstResult.get(0));
                }
            }
        }
    }

    @SuppressLint({"ClickableViewAccessibility", "ResourceType"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(_layout, container, false);

        // Display fragment_dialog
        if (_layout == R.layout.fragment_dialog) {
            _dialogText = view.findViewById(R.id.dialog_text);
            ImageView dialogSst = view.findViewById(R.id.dialog_sst);

            dialogSst.setOnClickListener(_view -> {
                SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this.getActivity());
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.GERMAN);

                mSpeechRecognizer.startListening(this.getActivity().getIntent());

                try {
                    startActivityForResult(intent, 10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        }
   }
}

我的建议是createSpeechRecognizer()startListening()的参数有问题。在关于StackOverflow的其他回答中,我看到的主要是:

SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.startListening(this);

嗯,当我使用this作为参数时,我会得到以下错误:

'createSpeechRecognizer(安卓.content.Context)' in '安卓.speech.SpeechRecognizer' cannot be applied to '(ch.yourclick.kitt.classes.Dialog)'

'startListening(安卓.content.Intent)' in '安卓.speech.SpeechRecognizer' cannot be applied to '(ch.yourclick.kitt.classes.Dialog)'

对于我使用的参数,我没有得到任何错误,但仍然怀疑这是否正确。如果是其他问题,请解释


共 (0) 个答案