您好我正在使用应用程序,它始终监听语音,每一秒钟用户定时器任务,每次调用SpeechRecognizer。当我启动SpeechRecognizer时,它会发出哔哔声。请帮我删除这个。我使用过amanager.setStreamMute(AudioManager.STREAM_ALARM,true);但这不是完美的解决方案。我的代码在这里。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
tts = new TextToSpeech(this, this);
amanager = (AudioManager) getSystemService(AUDIO_SERVICE);
startThread();
return super.onStartCommand(intent, flags, startId);
}
public void startThread() {
Thread thread = new Thread(this);
thread.start();
}
public void offSound() {
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}
public void onSound() {
amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (null != iAudioTimer) {
iAudioTimer.cancel();
iAudioTimer = null;
}
if(tt!=null){
tt.cancel();
tt=null;
}
}
public void run() {
try {
iAudioTimer = new Timer();
tt = new TimerTask() {
@Override
public void run() {
if (!isVoiceMatch) {
if (!amanager.isMusicActive()) {
raise();
}
}
}
};
iAudioTimer.schedule(tt, 0, 1 * 2000);
} catch (Exception e) {
Log.e("[AudioRecorder]: ", "run(): ", e);
}
}
public void raise() {
try {
handler.post(new Runnable() {
@Override
public void run() {
offSound();
SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
MyRecognitionListener listener = new MyRecognitionListener();
speech.setRecognitionListener(listener);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
speech.startListening(intent);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
class MyRecognitionListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
onSound();
}
@Override
public void onEvent(int eventType, Bundle params) {
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onReadyForSpeech(Bundle params) {
onSound();
}
@Override
public void onResults(Bundle results) {
}
@Override
public void onRmsChanged(float rmsdB) {
}
}
答案 0 :(得分:0)
这是Android中的Google api错误,我希望Google会尽快解决此问题。
现在,您可以制作自定义标记SpeechRecogniser.SET_VOICE_FLAG = false
并为screen_on和screen_off注册接收器:
registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_OFF));
这将有助于您删除长时间运行的进程和后台进程中的蜂鸣声,这是一种很好的做法。