0

I use an ontouch listener that detects when the user holds down a button and starts recording audio. However, I discovered that if the user just clicks on the button, the app crashes with this error

05-13 10:17:52.193    5370-5370/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.amosang.pushtest, PID: 5370
java.lang.RuntimeException: stop failed.
        at android.media.MediaRecorder.stop(Native Method)
        at com.example.amosang.pushtest.NewRequest.stopAudioRecord(NewRequest.java:251)
        at com.example.amosang.pushtest.NewRequest$2.onTouch(NewRequest.java:118)
        at android.view.View.dispatchTouchEvent(View.java:8155)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2166)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2166)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2166)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2166)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2166)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2166)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2311)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1606)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2617)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2259)
        at android.view.View.dispatchPointerEvent(View.java:8368)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4788)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4649)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4207)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4261)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4230)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4341)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4238)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4398)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4207)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4261)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4230)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4238)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4207)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6584)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6501)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6472)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6437)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6664)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:131)
        at android.app.ActivityThread.main(ActivityThread.java:5653)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

From what I discovered, it is either because the audio isn't recorded so there is nothing to stop, or because it isn't long enough so when the stop is called, it crashes.

This is the code I use

recordAudio.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            String fileTitle = requestTitle.getText().toString();
            Date date = new Date();
            String convDate = String.valueOf(date).replace(" ","");
            if(fileTitle.length()>0) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("PRESSED", "HOLD");
                        instructions.setVisibility(View.INVISIBLE);
                        startAudioRecord(fileTitle,convDate);

                        return true;
                    case MotionEvent.ACTION_UP:
                        Log.d("PRESSED", "RELEASED");
                        instructions.setVisibility(View.VISIBLE);
                        stopAudioRecord();
                        add.setVisibility(View.VISIBLE);
                        playRecord.setVisibility(View.VISIBLE);
                        stopPlayback.setVisibility(View.VISIBLE);
                        clearRecord.setVisibility(View.VISIBLE);
                        return true;
                }
            }else {
                Toast.makeText(getApplicationContext(),"Please enter a title",Toast.LENGTH_LONG).show();
            }
            return false;
        }
    });

Hope someone can help me with this. Should I set another click listener to detect if the user just clicks it and doesn't hold?

JianYA
  • 2,750
  • 8
  • 60
  • 136
  • You need to make sure the `MediaRecorder` is in an appropriate state before you try to stop it in your `stopAudioRecord()` method. – Mike M. May 13 '16 at 00:29
  • Are two events being fired? Both HOLD and RELEASED ? – Scary Wombat May 13 '16 at 00:30
  • Sorry I am not very familiar with states. Can you explain what that means? – JianYA May 13 '16 at 00:30
  • Yeah when I justs click it, both events are fired immediately. How can I stop that from happening? I still want the user to hold to record. – JianYA May 13 '16 at 00:34
  • Maybe have a variable that records the current state. Can not start if already started, can not stop in not already started – Scary Wombat May 13 '16 at 00:39
  • 1
    It seems `MediaRecorder` doesn't have methods analogous to `MediaPlayer`'s, as I assumed it would. Have a look at [this post](http://stackoverflow.com/questions/16221866/mediarecorder-failed-when-i-stop-the-recording). You pretty much just need to `catch` that `Exception`. – Mike M. May 13 '16 at 00:42
  • Thanks! It worked! How do I put your answer as correct? – JianYA May 13 '16 at 00:45
  • Actually, I think you should now have an option to mark that post as helpful, or something to that effect, which will close out your question. Glad you got it working. Cheers! – Mike M. May 13 '16 at 00:48

0 Answers0