2

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) {

    }

}
  • Hello Jasneshwar ... this is the api error of Google android and hope google will take a nice step to remove it soon . For the time you can make a custome Falg SpeechRecogniser.SET_VOICE_FLAG = false also register receiver for screen_on and Screen_off listener, registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_ON)); registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_OFF)); this will help u to remove the beep in long running process and background process this is gud practise. – user Jun 29 '14 at 17:08
  • @user I have edited the answer version of this comment. Please try to make use of the formatting tools SO offers and proofread posts before making them. It was extremely difficult to read. – Abandoned Cart May 03 '17 at 20:36

1 Answers1

0

This is a Google api error in Android and I hope Google will take time to resolve it soon.

For now, you can make a custom flag SpeechRecogniser.SET_VOICE_FLAG = false and register receivers for screen_on and screen_off:

registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_OFF));

This will help u to remove the beep in long running processes and background processes, which is good practice.

For more information, read Android Speech Speech Recognition: Repeated Calling of SpeechRecognizer.startListening() fails on JB 4.1.2

Community
  • 1
  • 1
user
  • 245
  • 1
  • 4
  • 12
  • Thanks for your valuable response, please tell me if any long time running service can be stop if android garbage collector acquire all the resources from it . .... i have started service as following. @Override public int onStartCommand(Intent intent, int flags, int startId) { tts = new TextToSpeech(this, this); amanager = (AudioManager) getSystemService(AUDIO_SERVICE); startThread(); return START_NOT_STICKY; } – Janeshwar Yashpal Jun 29 '14 at 17:17