0

I have a start button that initiates audio recording, and a stop button that ends the process.

This is the java code of the start button:

    startbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //My code
        }
    });

This is the java code for the stop button:

    stopbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //My code
        }
    });

This is their respective XML layout if needed:

<Button
    android:id="@+id/btnRecord"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="100dp"
    android:layout_marginTop="120dp"
    android:layout_marginBottom="100dp"
    android:text="Start Recording" />

<Button
    android:id="@+id/btnStop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="100dp"
    android:layout_marginBottom="40dp"
    android:text="Stop Recording" />

So far i use "setEnabled(True)" or "false" to manage the app. Instead i need to merge these states in a single button so it can be "START/STOP" for a more simplistic UI... There are more buttons but i will fix them on my own hopefully, after learning the how to. Cheers!

PS: I typed "//My code" instead of the actual code in order to save space. I can provide my code if needed.

UPDATE: My logcat:

2019-02-27 13:17:29.634 8550-8550/com.android.greg.garec E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.greg.garec, PID: 8550
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaRecorder.stop()' on a null object reference
    at com.android.greg.garec.MainActivity$1.onClick(MainActivity.java:96)
    at android.view.View.performClick(View.java:6897)
    at android.widget.TextView.performClick(TextView.java:12693)
    at android.view.View$PerformClick.run(View.java:26101)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  • 1
    either u can overlay 2 buttons and manage its visibility and clickable property to achieve this. or on click change its behaviour according to the state of player – Tomin B Azhakathu Feb 26 '19 at 11:41
  • Use a single button and a boolean variable for the state of the button (initially: recording=false). When button is clicked, set recording=true and change image of the button and vice versa. – unlut Feb 26 '19 at 11:43
  • https://stackoverflow.com/a/17647520/10695093 try this – Akash Suresh Dandwate Feb 26 '19 at 12:29

3 Answers3

0

Hey if you want to add both actions in one button then its simple just use boolean flag for the state for example set flag isRecording=true if recording started and set isRecording=false when recording stop. (Note: Set isRecording global key into the class)

 startbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     if(isRecording){
         stopRecording();
         isRecording=false;
         startbtn.settext("Start Recording")
       }else{
         startRecording();
         isRecording=true;
         startbtn.settext("Stop Recording")
       }

    }
});
Mahesh Keshvala
  • 1,349
  • 12
  • 19
  • `startbtn` and `commonBtn` is same. – Pratik Butani Feb 26 '19 at 11:46
  • yes he just mention that i only want a one button so that i mention that btn as commonBtn – Mahesh Keshvala Feb 26 '19 at 11:48
  • This looks promising, i will test it tomorrow and i hope it works :D –  Feb 26 '19 at 18:35
  • @Gregory if you find this answer helpful then please mark as right and upvote it. – Mahesh Keshvala Feb 27 '19 at 06:38
  • I haven't gotten around it yet to see if it works because i am facing some other problems i need to attend to. Don't worry, i never forget the people who helped me. –  Feb 27 '19 at 07:01
  • have you defined key isRecording key above class? – Mahesh Keshvala Feb 27 '19 at 10:25
  • Yes "boolean isRecording = true; " in the outer scope of the main class to make it global. There is no syntax error but the app crashes when i press buttons. I test change the value of isRecording to false or true in different places. Sometimes the app might Start recording but crash once i press stop. I can edit the question and post the entire file if you want to check it out yourself. –  Feb 27 '19 at 11:00
  • There is no compilation error, it runs but crashes once i try to record. –  Feb 27 '19 at 11:04
  • but there is log into android studio logcat please check that log in that run time error display. – Mahesh Keshvala Feb 27 '19 at 11:07
  • I posted the logcat. –  Feb 27 '19 at 11:19
  • that is android media player error compiler not found media player instance have you define media player global? – Mahesh Keshvala Feb 27 '19 at 11:23
  • Something really strange happened. I defined isRecording as false instead of true, and it appears to be working. However my play/stop playing buttons don't work now while they work perfectly if i live start recording/stop recording buttons separate. It is truly bizarre. –  Feb 27 '19 at 11:39
  • Never mind brother i fixed it! in my previous code, the one i copied-pasted i had been using setEnabled(True) or false too much, that was the problem. Your answer works but isRecording has to be globally set on FALSE instead of true by definition. Thanks! –  Feb 27 '19 at 11:55
  • Gregory you are right we need to set the key to false by default sorry i forgot to mention that in my answer i apologize for that. and thanks for accepting my answer. – Mahesh Keshvala Feb 27 '19 at 12:01
-1

use Custom Checkbox for Record and Stop. and check if it is Checked or not

-1

Use can use a single buttom with .setTag()

singleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
 if(singleBtn.getTag().toString().equals("Stop")){
     stopRecording();
     startbtn.settext("Start Recording")
     singleBtn.setTag("Record");
   }else if(singleBtn.getTag().toString().equals("Record")){
     startRecording();
     startbtn.settext("Stop Recording")
     singleBtn.setTag("Stop");
   }

}
});
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437