Home > Net >  MediaRecorder.Prepare() IllegalStatEexception
MediaRecorder.Prepare() IllegalStatEexception

Time:07-25

I am trying Android voice recording app by Official Link and it gives me crash at recorder.start(); because prepare statement not executed.

The only change i made in my app is location where i need to store audio recording where official documentation using

 // Record to the external cache directory for visibility
        fileName = getExternalCacheDir().getAbsolutePath();
        fileName  = "/audiorecordtest.3gp";

My Code

String subfolder = "Exotel/Media/Audio/Voice Messages/Audio Temp";
                        String filename = "Exotel" "_Voice" System.currentTimeMillis() ".3gp";
                        String TempPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)   File.separator   subfolder;
                        File Dir = new File(TempPath);
                        if (!Dir.exists()){
                            Dir.mkdirs();
                        }
                        TempPath = TempPath File.separator filename;

                        Log.d(TAG, "onTouch: Temp Path " TempPath);
                        recorder = new MediaRecorder();
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        recorder.setOutputFile(TempPath);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        try {
                            recorder.prepare();
                        } catch (IOException e) {
                            Log.e(TAG, "prepare() failed " e.getMessage());
                        }

                        recorder.start();

If i use fileName = getExternalCacheDir().getAbsolutePath(); fileName = "/audiorecordtest.3gp"; then my code is working perfectly. Also if i change the extension to .mp3 then it also working in this case. I don't understand what exactly wrong in my code. Please help.

CodePudding user response:

Use Dir instead TempPath in setOutputFile as below

recorder.setOutputFile(Dir.absolutePath);

Hope it will work

CodePudding user response:

private var myAudioRecorder: MediaRecorder? = null
private var outputFile: String? = null

Call setupAudioRecordSource method in onCreate of your activity

private fun setupAudioRecordSource() {
        outputFile = createPathToSaveAudio()
        myAudioRecorder = MediaRecorder()
        myAudioRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
        myAudioRecorder?.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
        myAudioRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
        myAudioRecorder?.setOutputFile(outputFile)
     
    }

private fun createPathToSaveAudio(): String? {
    val storageDir = getAttachmentFilePath()
    return File(
        storageDir,
        "recordedAudio_"   Calendar.getInstance().timeInMillis.toString()   ".3gp"
    ).absolutePath
}

fun Context.getAttachmentFilePath(): String {
    val storageDir = File(
        this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
        "Attachment"
    )
    if (storageDir?.exists() == false) {
        storageDir.mkdirs()
    }
    return storageDir.absolutePath
}

When I start recording audio call below method

   private fun recordAudio() {
        try {
            myAudioRecorder?.prepare()
            myAudioRecorder?.start()
        } catch (e: IllegalStateException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        } catch (e: IOException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }
    }

After recording call below method in stop button

   private fun stopAudio() {
        if (myAudioRecorder != null) {
            try {
                myAudioRecorder?.stop()
                myAudioRecorder?.reset()
                myAudioRecorder = null
            } catch (e: java.lang.IllegalStateException) {
                //  it is called before start()
                e.printStackTrace()
            } catch (e: RuntimeException) {
                // no valid audio/video data has been received
                e.printStackTrace()
            }
        }
    }
  • Related