1

Im working on a personal assistant using pocketsphinx speech recognizer in android. This is the way my app works every time a special word is heard the personal assistant will answer back and do a task. I have been having some problems with the releasing of the microphone. I dont know if it is a bug. It only happens sometimes when I close the application the microphone is still looking for that word and answering using text to speech. Even though screen is off. When I tried to record a video it says microphone is used by another application. So I have to open my app again and close the app to release the microphone. In my knowledge the only lifecycles to release resources are onStop, onPause, and onDestroy. It cannot be my phone that is malfunctioning I have tested the app with two different phones and still sometimes it happens in bot of them. Any help would be appreciated. This is the way Im releasing the microphone, camera and the text to speech. Thanks in advance

private edu.cmu.pocketsphinx.SpeechRecognizer recognizer;



   @Override
            public void onPause() {
                super.onPause();
           if (tts != null) {
              tts.shutdown();
               }
                if (camera != null) {
                    camera.release();
                    camera = null;
                }
                if (recognizer != null) {
                    recognizer.stop();
                    recognizer.cancel();
                    recognizer.shutdown();
                    recognizer = null;
                }

            }

        @Override
            protected void onStop() {
                super.onStop();
           if (tts != null) {
                tts.shutdown();
                 }
                if (camera != null) {
                    camera.release();
                    camera = null;
                }
                if (recognizer != null) {
                    recognizer.cancel();
                    recognizer.shutdown();
                    recognizer = null;
                }
            }
   @Override
    public void onDestroy() {
        super.onDestroy();
        if (tts != null) {
            tts.shutdown();
        }
        if (camera != null) {
            camera.release();
            camera = null;
        }
        if (recognizer != null) {
            recognizer.cancel();
            recognizer.shutdown();
        }

    }
sptBot
  • 51
  • 8

1 Answers1

1

Try overriding the onbackpressed method and post ur recorder stop code there. And then finally add in the end youractivity.finish; This shall destroy ur activity as soon as back is pressed and should stop the recorder

  • I will try that right now. I will test it and let you know. Is there any method also for the homebutton press just like the onbackpressed? Thanks – sptBot Jun 21 '17 at 23:45
  • Try onUserLeaveHint() this will take instances of the app being interrupted in any way such as attending a call, pressing hone button etc. – Wali Muhammad Khubaib Jun 21 '17 at 23:50
  • I already implemented the onBackPressed method. I will have to test the onbackpressed for some time. Like I said it only happens sometimes. Im trying to find out where is the microphone not getting released. I thought the onStop, onPause, and onDestroy was sufficient to release resources for other apps. onUserLeaveHint() it will also handle the onBackpressed based on [link](https://stackoverflow.com/questions/6612058/android-onbackpressed-onuserleavehint) @AndyW answer? – sptBot Jun 22 '17 at 00:12
  • Yes it will, like i said all instances of user leaving the app – Wali Muhammad Khubaib Jun 22 '17 at 00:14
  • I think u can also try removing the if conditions onUserLeaveHint n shut them down anyway. Maybe at sm point the condition may not get true. – Wali Muhammad Khubaib Jun 22 '17 at 00:16
  • Ok. I think that the app will crash. The problem is if the object is null the application will crash. Even thought the application is in the process of closing the message that the app crashed will still appear on the user screen and that is not what I want. Thanks for your answer it was really helpful. – sptBot Jun 22 '17 at 00:24
  • Oh I see, no worries glad i could help :) – Wali Muhammad Khubaib Jun 22 '17 at 00:26