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?
have you also requested runtime permissions?
also have a look at this article
EDIT
try this snippet