-1

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)   
  • 2
    **Don't ignore exceptions.** In order to call `start()`, the recorder must be in state "Prepared", which is established by a *successful* call to `prepare()`, but `prepare()` likely failed (see your log), the code ignored the exception (after logging it), so the code attempted to `start()` anyway, causing that error. --- In short: Don't call `start()` if `prepare()` failed. – Andreas Feb 05 '20 at 07:28
  • 2
    Andreas is correct: you should be calling `start()` *INSIDE* the try/catch block, *AFTER* you've successfully completed `prepare()`. ALSO: Look here: [Why does the start() method of MediaRecorder throw an IllegalStateException?](https://stackoverflow.com/questions/5177039/why-does-the-start-method-of-mediarecorder-throw-an-illegalstateexception) – FoggyDay Feb 05 '20 at 07:29

2 Answers2

0

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.

0

replace fileName = "/audio.3gp";

with

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