1

So I am using SpeechRecognizer along with RecognitionListener to recognize user speech. It works fine but the problem is when I tested it I noticed that it doesn't call onEndOfSpeech after I finishes talking. It waits around 7 to 10 seconds and then onEndOfSpeech and onResult are called. I don' know the reason for this delay.

Here is the code:

    public class MainActivity extends AppCompatActivity  {
    protected static final int REQUEST_OK = 1;
    SpeechRecognizer sr;
    TextView mText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mText = (TextView) findViewById(R.id.text1);
        RecognitionListener recognitionListener=new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {
           Toast.makeText(MainActivity.this,"Start takling",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float v) {

            }

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

            }

            @Override
            public void onEndOfSpeech() {
                Toast.makeText(MainActivity.this,"onEndOfSpeech",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onResults(Bundle bundle) {
                Toast.makeText(MainActivity.this,"onResults",Toast.LENGTH_SHORT).show();
                ArrayList<String> voiceResults =
                        bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                if (voiceResults != null) {
                    String text = voiceResults.get(0);
                    Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onPartialResults(Bundle bundle) {

            }

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

            }
        };
        sr = SpeechRecognizer.createSpeechRecognizer(this);
        sr.setRecognitionListener(recognitionListener);

        findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                sr.startListening(intent);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        sr.destroy();
    }
    }

activity_main.xml:

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="..." ></TextView>

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" ></ImageButton>

        </RelativeLayout>
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
jelmood jasser
  • 878
  • 1
  • 13
  • 30

0 Answers0