• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor Thread terminate is not working properly

I am trying to create a thread program which will keep on playing two sound in 1 second interval. When back button is pressed the app will close and the sounds will stop.

Initially when I was pressing back button the app was closing but the sound did not stop hence overridden the method onBackPressed and put thread.terminate method in onBackPressed method. That caused sound to stop but after app was closed error message is popping up. Screenshot of the error is given elow
k8t_HCe4GePiaEJDBT6eszXNUj9TtJTU-Arg9MprU26NZxUJ00kepIH3Q2lNJiv6X3UYlh6aORwAi_TrxqxpCzdEAO9KV34vHolyM_CRwE1KpzluwwtIYbbfGVCZHpWAqonxEsVOry9lBwO_L3oxjHay7YfNecD9nMekdszn0vd_AdPBfgT-xxHN5Pc3nczTHuYim5F96FOHy1QbZ7y2LxbTD-vzL9Qs9RzbLG1e7nyxUiEaYHHZtfKCxzapu0YmdJPyvtYuVEC-cS0e0mxg-pglkNH5nm-mg1cWOPoxN6YC1xf4IBZYyy8A6R3POAnREawUrvXtTQj6vgbsVLpknOAwG0l5KSt6i5ZaiFK0rcrUhYJam-X3YL9C9FSrreOKcWr7U8Vt_vgn-n2K1Ju5FopljefV8hO38bpH-j8-tlplyDjQL8P_LAsDOEyp4RU5f8AKYNQvsHv3qLf6xGiG4v73Hjc0bbJHO6Ph-VuNygjDtqck-HfkDet8cujVAzk_oEtrQO_7BsbkQcLy08G-UCgQBysF-VYf-L1Z6cKFvZhVAPBaR2nVRuEE2FWiSZQD-ctSNsMYJGSy76NDs3txruR1UrbFaa3dsK4n9QY=w383-h710-no


Below is the code. I have placed try-catch and Log.v in every position but no error received in the log. But found below error, not sure from where this error is coming as this is not associated to any log tag. Below is the error details

onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)
at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)
at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)
at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)

Code is shown below....


Java:
public class MainActivity extends AppCompatActivity {
    MediaPlayer mp;
    MediaPlayer mq;
    Task tsk;
    Thread myThread;
    TextView txt;
    int iTest,iTerminate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iTest = 0;
        iTerminate = 1;
        txt = (TextView)findViewById(R.id.txtVwTest);
        mp = MediaPlayer.create(this, R.raw.sound_1);
        mq = MediaPlayer.create(this, R.raw.sound_2);
        tsk= new Task();
        myThread=new Thread(tsk);
        myThread.setDaemon(false);
        myThread.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (myThread!=null)
        {
            try {
                iTerminate = 0;
                myThread.interrupt();
                myThread = null;
            }
            catch (Exception ex) {
                Log.v("ThreadStopLog",ex.getMessage());
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (myThread!=null)
        {
            try {
                iTerminate = 0;
                myThread.interrupt();
                //myThread.stop();
                myThread = null;
            }
            catch (Exception ex) {
                Log.v("ThreadStopLog",ex.getMessage());
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        if (myThread!=null)
        {
            try {
                iTerminate = 0;
                myThread.interrupt();
                myThread = null;
            }
            catch (Exception ex) {
                Log.v("ThreadStopLog",ex.getMessage());
            }
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (myThread!=null)
        {
            try {
                iTerminate = 0;
                myThread.interrupt();
                myThread = null;
            }
            catch (Exception ex) {
                Log.v("ThreadStopLog",ex.getMessage());
            }
        }
        super.onDestroy();
    }

    class Task implements Runnable {
        @Override
        public void run() {
            if (Thread.interrupted()==true)
            {
                iTerminate = 0;
                return;
            }
            while (iTerminate == 1) {
                iTest++;
                //txt.setText(iTest);
                Log.v("ThreadTest",Integer.toString(iTest));
                try {
                    mp.start();
                }
                catch (Exception ex)
                {
                    Log.v("SoundError --> mp",ex.getMessage().toString());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.v("SleepError --> mp-sleep",e.getMessage().toString());
                }
                try {
                    //mp.stop();
                    mq.start();
                }
                catch (Exception ex)
                {
                    Log.v("SoundError --> mq",ex.getMessage().toString());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.v("SleepError --> mq-sleep",e.getMessage().toString());
                }
                try {
                    mq.stop();
                }
                catch (Exception ex)
                {
                    Log.v("SoundError --> mq-stop",ex.getMessage().toString());
                }
            }
        }
    }
}
 
To start with, using Log statements is an inefficient way to debug your app. Run the app in debug mode, and use breakpoints to verify code execution.

Memory problems can occur with MediaPlayer if you don't call release() after calling stop(). Try that.

If you Google the exception you're getting -

Code:
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.

it also turns up some useful links, like this:

https://stackoverflow.com/questions/43900539/google-play-example-error-code-393238
https://stackoverflow.com/questions...studio-with-error-session-android-error-launc
 
I have used mp.stop and mp.release in onBackPressed, onPause etc. But I still am getting same error message.

Modified code is given below.

Java:
@Override
    public void onBackPressed() {
        if (myThread!=null)
        {
            try {
                mp.stop();
                mp.release();
                iTerminate = 0;
                myThread.interrupt();
                //myThread.stop();
                myThread = null;
            }
            catch (Exception ex) {
                Log.v("ThreadStopLog",ex.getMessage());
            }
        }
        super.onBackPressed();
    }

In logcat I am getting below error

onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)
at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)
at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)
at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)

Also the link given above seems not related to my issue.
 
Back
Top Bottom