Application error when the recorder should stop

I have an application uploaded to Google Play. This is a link.

It has not been tested on many devices, however errors are fairly common. The user today sent me a message that the application will crash if the switch is on and the button is pressed, but 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:

The application does not always crash. Sometimes this happens, sometimes it is not. This only happens when the switch is on. If I touch and hold the button, it works fine, but if I touch it only for a moment, it falls. I am using Xperia S 4.1.2

I tried it on my phone, I just touched the button, and did not hold it, and it worked 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 logcat says that 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 suppose the problem is that it only touches the button for a moment, so before startRecording is fully called, stopRecoring is already called so that it will work because startRecording has not yet been fully scheduled. If so, how can I fix it? If this is not so, then what is wrong? And why such an error appears on another phone, but not mine?

+4
source share
1 answer

According to the documentation, this is normal behavior:

public void stop ()

Added to API Level 1 Stop recording. Call it after the start (). once the recording is stopped, you will have to configure it again, as if it had just been built. Note that a RuntimeException is deliberately thrown into the application if there is no valid audio / video data received when stop () is called . This happens if stop () is called immediately after starting (). Application execution failed to clean up the output file (delete output file, for example), because the output file is not configured correctly when this happens.

So you can just add try catch to your stop call

 if (null != recorder) { try{ recorder.stop(); }catch(RuntimeException ex){ //Ignore } ... } 
+11
source

Source: https://habr.com/ru/post/1498840/


All Articles