0

我正在使用语音识别器意图来获取用户输入并将其翻译成文本。但是,我希望 Intent 不断接受用户输入并将其翻译成文本,以查看用户是否说出了某个词。我的代码能够做到这一点,但每次我的应用程序开始监听输入时,手机都会发出一声短促的哔哔声,表示它已准备好输入。

我想知道是否有任何方法可以在每次识别器准备好再次收听时从播放中删除声音。

这是我的代码:

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends Activity implements OnClickListener 
{

       //private TextView mText;
       private SpeechRecognizer sr;
       private static final String TAG = "MyStt3Activity";
       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                ToggleButton speakButton = (ToggleButton) findViewById(R.id.CarMode);     
                //mText = (TextView) findViewById(R.id.textView1);     
                speakButton.setOnClickListener(this);
                sr = SpeechRecognizer.createSpeechRecognizer(this);       
                sr.setRecognitionListener(new listener());        
       }

       class listener implements RecognitionListener          
       {
                public void onReadyForSpeech(Bundle params)
                {
                         Log.d(TAG, "onReadyForSpeech");
                }
                public void onBeginningOfSpeech()
                {
                         Log.d(TAG, "onBeginningOfSpeech");
                }
                public void onRmsChanged(float rmsdB)
                {
                         Log.d(TAG, "onRmsChanged");
                }
                public void onBufferReceived(byte[] buffer)
                {
                         Log.d(TAG, "onBufferReceived");
                }
                public void onEndOfSpeech()
                {
                         Log.d(TAG, "onEndofSpeech");
                }
                public void onError(int error)
                {
                         Log.d(TAG,  "error " +  error);
                         Toast.makeText(getApplicationContext(), "What was that?", Toast.LENGTH_SHORT).show();
                         sr.cancel();
                         speechIntent();
                }
                public void onResults(Bundle results)                   
                {
                         //String str = new String();
                         Log.d(TAG, "onResults " + results);
                         ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                         Toast.makeText(getApplicationContext(), data.get(0), Toast.LENGTH_SHORT).show();
                         sr.cancel();
                         speechIntent();
                         //mText.setText("results: "+String.valueOf(data.size()));        
                }
                public void onPartialResults(Bundle partialResults)
                {
                         Log.d(TAG, "onPartialResults");
                }
                public void onEvent(int eventType, Bundle params)
                {
                         Log.d(TAG, "onEvent " + eventType);
                }
       }
       public void onClick(View v) {
                if (v.getId() == R.id.CarMode) 
                {
                    speechIntent();
                }
       }

       public void speechIntent()
       {
           Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"com.example.freestyleandroid");

                    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                         sr.startListening(intent);
                         Log.i("111111","11111111");
       }
    }
4

1 回答 1

0

这应该为您解决问题。将此添加到您的RecognitionListener.onReadyForSpeech()方法中。

mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
于 2013-09-09T03:27:59.983 回答