我正在使用语音识别器意图来接受用户输入并将其转换为文本。但是,我希望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");
}
}
最佳答案
这应该为您解决问题。将此添加到您的RecognitionListener.onReadyForSpeech()
方法。
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
关于java - 语音识别器声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18691037/