Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashes when recorder is supposed to stop

I have an app uploaded on google play. This is the link.

It hasn't been tested on many devices yet so errors are pretty common. A user messaged me today saying that the app crashes if the togglebutton is turned ON and the button is only pressed, not held.

This is the logcat file that he sent me:

E/MessageQueue-JNI(31135): java.lang.RuntimeException: stop failed.
E/MessageQueue-JNI(31135): at android.media.MediaRecorder.stop(Native Method)
E/MessageQueue-JNI(31135): at com.whizzappseasyvoicenotepad.MainActivity.stopRecording(MainActivity.java:183)

Quote:

App doesn't always crash. Sometimes it does, sometimes it doesn't. It only happens when the togglebutton is ON. If I touch and hold the button it works fine but if I only touch it for a moment it crashes. I'm using Xperia S 4.1.2

I tried this on my phone, I only touched the button instead of holding it and it worked perfectly fine, I don't know why this is happening on his phone.

This is the code for onTouchListener:

recBtn.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    startRecording();
                }
                else if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    stopRecording();
                    nameAlert();
                }
            return true;
            }
        });

And the logcat says the problem occurs when stopRecording is called, so here is the stopRecording method:

public void stopRecording() {
    final ImageButton recBtn = (ImageButton) findViewById(com.whizzappseasyvoicenotepad.R.id.recButton);
    final ToggleButton tBtn = (ToggleButton) findViewById(R.id.tBtn1);
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release(); 
        recorder = null;

        recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
        stopTimer();
        tBtn.setEnabled(true);
    }
}

I'm guessing that the problem is that he only touches the button for a moment, so before startRecording is completely called, the stopRecoring is already called so it crashes because startRecording wasn't even completely intiated yet. If that's the case, how can I fix it? If it isn't the case, what's wrong then? And why would an error like this appear on another phone but not mine?

like image 817
Guy Avatar asked Aug 25 '13 14:08

Guy


1 Answers

According to documentation, this is the normal behavior:

public void stop ()

Added in API level 1 Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed. Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

So you can just add a try catch to your stop call

if (null != recorder) {
   try{     
      recorder.stop();
   }catch(RuntimeException ex){
   //Ignore
   }
   ...
}
like image 71
Raúl Juárez Avatar answered Sep 29 '22 19:09

Raúl Juárez