设置SpeechRecognizer的录音限制时间

时间:2020-02-04 14:11:59

标签: android android-speech-api

我完全是Android开发的初学者,如何设置录音的时限?我想将最长录制时间设置为10秒,我需要在代码中添加什么。以下是我的编码部分,希望有人可以帮助我解决这个问题。谢谢

final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
                Locale.getDefault());
 findViewById(R.id.btnRecord).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_UP:
                        mSpeechRecognizer.stopListening();
                        //when the user removed the finger
                        message.setHint("You will see input here");
                        break;

                    case MotionEvent.ACTION_DOWN:
                        //finger is on the button
                        a.speak("Record", TextToSpeech.QUEUE_FLUSH,null);
                        Toast.makeText(getBaseContext(), R.string.record, Toast.LENGTH_SHORT).show();
                        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
                        message.setText("");
                        message.setHint("Listening...");
                        break;
                }
                return false;


            }
        });

在此处添加代码?设置在何处以及如何执行的最长时间?

  mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

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

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                //getting all the matches
                ArrayList<String> matches = bundle
                        .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                //displaying the first match
                if (matches != null)
                    message.setText(matches.get(0));
            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

            @Override
            public void onEvent(int i, Bundle bundle) {

            }
        });

0 个答案:

没有答案