java.lang.IllegalStateException at android.media.MediaRecorder.start(Native Method)

前端 未结 5 2043
别那么骄傲
别那么骄傲 2021-01-14 12:22

I want to make a voice recorder app but it crashes when i click the \"Start Recording\" button. I get an error saying java.lang.IllegalStateException at android.media.Media

5条回答
  •  独厮守ぢ
    2021-01-14 13:02

    Call this after setOutFormat() but before prepare().


    this is just what my android studio docs dialog says while i write this method name. the point is that you should call this method just before prepare().
    here is an example:

    private void startRecording() {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MediaMaster/Dub/");
        Log.i(TAG, "startRecording: creating output file " + outputFolder.mkdirs());
        File output = new File(outputFolder.getAbsolutePath()+"out" + new Date().getTime() + ".3gpp");
        mediaRecorder.setOutputFile(output.getAbsolutePath());
        mediaRecorder.setMaxDuration(3000);
        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            Log.e(TAG, "startRecording: ", e);
        }
        mediaRecorder.start();
    }
    


提交回复
热议问题