我正在使用语音识别器意图来获取用户输入并将其翻译成文本。但是我希望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");
}
}
答案 0 :(得分:0)
这应该可以帮到你。将其添加到您的RecognitionListener.onReadyForSpeech()
方法。
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);