Java 语音识别器声音
我使用语音识别器来获取用户输入并将其转换为文本。然而,我想要的意图是不断地获取用户输入并将其翻译成文本,以查看用户是否说了某个单词。我的代码可以做到这一点,但每次我的应用程序开始监听输入时,手机都会发出一声简短的哔哔声,表示它已准备好输入 我想知道是否有任何方法可以在识别器准备再次收听时删除播放的声音 这是我的密码:Java 语音识别器声音,java,android,audio,android-intent,speech-recognition,Java,Android,Audio,Android Intent,Speech Recognition,我使用语音识别器来获取用户输入并将其转换为文本。然而,我想要的意图是不断地获取用户输入并将其翻译成文本,以查看用户是否说了某个单词。我的代码可以做到这一点,但每次我的应用程序开始监听输入时,手机都会发出一声简短的哔哔声,表示它已准备好输入 我想知道是否有任何方法可以在识别器准备再次收听时删除播放的声音 这是我的密码: import java.util.ArrayList; import android.app.Activity; import android.content.Intent; i
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");
}
}
import java.util.ArrayList;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.speech.RecognitionListener;
导入android.speech.RecognizerIntent;
导入android.SpeechRecognizer;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Toast;
导入android.widget.ToggleButton;
公共类MainActivity扩展活动实现OnClickListener
{
//私有文本查看多行文本;
私人语音识别器sr;
私有静态最终字符串TAG=“MyStt3Activity”;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton speakButton=(ToggleButton)findViewById(R.id.CarMode);
//mText=(TextView)findViewById(R.id.textView1);
speakButton.setOnClickListener(此);
sr=SpeechRecognizer.createSpeechRecognizer(此);
sr.setRecognitionListener(新的listener());
}
类侦听器实现识别侦听器
{
ReadyForSpeech上的公共void(Bundle参数)
{
Log.d(标签“onReadyForSpeech”);
}
开始时的公共无效fSpeech()
{
Log.d(标签“onbeginingofspeech”);
}
在RMSCHANGED上的公共无效(浮动rmsdB)
{
Log.d(标签“onRmsChanged”);
}
已接收公共无效onBufferReceived(字节[]缓冲区)
{
Log.d(标记“onBufferReceived”);
}
公共无效onEndOfSpeech()
{
Log.d(标签“onEndofSpeech”);
}
公共无效onError(内部错误)
{
日志d(标签“错误”+错误);
Toast.makeText(getApplicationContext(),“那是什么?”,Toast.LENGTH\u SHORT.show();
高级取消();
演讲意图();
}
公共结果(捆绑结果)
{
//String str=新字符串();
Log.d(标签“onResults”+结果);
ArrayList data=results.getStringArrayList(SpeechRecognitor.results\u RECOGNITION);
Toast.makeText(getApplicationContext(),data.get(0),Toast.LENGTH_SHORT.show();
高级取消();
演讲意图();
//setText(“结果:+String.valueOf(data.size()));
}
public void onPartialResults(Bundle partialResults)
{
Log.d(标签“onPartialResults”);
}
public void onEvent(int eventType,Bundle参数)
{
Log.d(标记“onEvent”+事件类型);
}
}
公共void onClick(视图v){
if(v.getId()==R.id.CarMode)
{
演讲意图();
}
}
公开无效言论意图()
{
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(RecognizerIntent.EXTRA语言模型,RecognizerIntent.LANGUAGE模型自由形式);
intent.putExtra(RecognizerIntent.EXTRA_调用_包,“com.example.freestyleandroid”);
intent.putExtra(识别器intent.EXTRA_MAX_结果,5);
高级督察(意向);
Log.i(“111111”、“11111111”);
}
}
这应该能帮到你。将其添加到RecognitionListener.onReadyForSpeech()方法中
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
有没有更好的解决办法?虽然它“工作”使声音静音,但它也使整个流静音。4年后有点“黑”。