MediaRecorder.prepare() keeps failing and throwing an exception

168 views Asked by At

I have the following function below for my start record button to record audio on my Samsung S20 FE:

private fun startRecording (button: Button) {
        recorder = MediaRecorder().apply {
            setAudioSource(MediaRecorder.AudioSource.MIC)
            setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)

            setOutputFile("/Internal storage/recorded/asdf.mp4")
            setAudioEncoder(MediaRecorder.AudioEncoder.AAC)

            try {
                prepare()
                start()
                button.setText("mic started")
            } catch (er: IllegalStateException) {
                button.setText("IllegalStateException")
                er.printStackTrace()


            } catch (e: IOException) {
                //System.exit(0)
                button.setText("prepare failed")
                Log.e(LOG_TAG, "media recorder prepare() failed")
            }


        }
    }

This function works up until the prepare() part, at which point it throws an exception and fails (I'm currently using the button to debug).

I've looked online, and people keep saying that AndroidManifest.xml must be missing the following permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

But mine xml file has those permissions. What should I do to fix this error?

1

There are 1 answers

3
zaid khan On

have you also requested runtime permissions?

private fun requestPermission(){
 val hasPermission = ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) == PackageManager.PERMSSION_GRANTED

if(!hasPermission){
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){ permitted -> 
     if(!permitted){
       }
}
}
requestPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
}
}

also have a look at this article

EDIT

try this snippet

 private MediaRecorder rec;
       String file_path=getApplicationContext().getFilesDir().getPath();

            File file= new File(file_path);

            Long date=new Date().getTime();
            Date current_time = new Date(Long.valueOf(date));

            rec=new MediaRecorder();

            rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            rec.setAudioChannels(1);
            rec.setAudioSamplingRate(8000);
            rec.setAudioEncodingBitRate(44100);
            rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            if (!file.exists()){
                file.mkdirs();
            }

            String file_name=file+"/"+current_time+".3gp";
            rec.setOutputFile(file_name);

         try {
                    rec.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(Recording_Service.this,"Sorry! file creation failed!"+e.getMessage(),Toast.LENGTH_SHORT).show();
                    return;
                }
                rec.start();

           rec.stop();
           rec.reset();