Hello I am working on app which listen voice all the time, for that i user Timer task in each one second and this call SpeechRecognizer each time . When i start SpeechRecognizer it make a beep voice . please help me to remove this . i have used amanager.setStreamMute(AudioManager.STREAM_ALARM, true); but this is not perfect solution . my code is here.
@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) {
}
}