'How to solve java.lang.IllegalStateException while recording audio in android app

I am creating an app to record audio. I have created a fragment named 'Record' which contains a button to record audio. This is the code of fragment

public class Record extends Fragment {

private Button ButtonRecord;
private MediaRecorder recorder;
private  String fileName = null;
private static final String LOG_TAG = "Record_Log";
private int ClickCount = 0;
private StorageReference Storage;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_record,container,false);

    ButtonRecord = v.findViewById(R.id.button_record_record);
    fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    fileName = "/audio.3gp";
    Storage = FirebaseStorage.getInstance().getReference();

    ButtonRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ClickCount++;
            if(ClickCount%2 == 1) {
                startRecording();
                ButtonRecord.setText("Stop");
            }
            else if(ClickCount%2 == 0) {
                stopRecording();
                ButtonRecord.setText("Record");
            }
        }
    });

    return v;
}

This is the code for start-stop recorder.

private void startRecording() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(fileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        recorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    recorder.start();
}

private void stopRecording() {
    recorder.stop();
    recorder.release();
    recorder = null;
}

When I try to press the record button the app crashes and following error is shown. The error is shown in a pop-up.

java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.example.afinal.Record.startRecording(Record.java:68)
at com.example.afinal.Record.access$100(Record.java:19)
at com.example.afinal.Record$1.onClick(Record.java:42)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)   


Solution 1:[1]

Thank you all for the answers. I actually figured out where the problem was. I assigned fileName some value twice. I combined it together with a + and it worked.

Solution 2:[2]

replace fileName = "/audio.3gp";

with

fileName = fileName + "/audio.3gp";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nahush Amane
Solution 2 vaidik limbachiya