On Android 11 my MediaRecorder fails to initialize. I suspect the problem is related to scopedstorage, but I have been unable to figure out the cause. I am using MediaRecorder to record audio from the microphone. I extract the amplitude from the audio, so I have no intention to keep the file, that is why the path is /dev/null
var mRecorder: MediaRecorder? = null
if (mRecorder == null) {
mRecorder = MediaRecorder()
mRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
mRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
mRecorder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
mRecorder!!.setOutputFile("/dev/null")
try {
mRecorder!!.prepare()
} catch (ioe: IOException) {
Log.e("[Monkey]", "IOException: " + Log.getStackTraceString(ioe))
} catch (e: SecurityException) {
Log.e("[Monkey]", "SecurityException: " + Log.getStackTraceString(e))
}
try {
mRecorder!!.start()
} catch (e: SecurityException) {
Log.e("[Monkey]", "SecurityException: " + Log.getStackTraceString(e))
}
The crash is at MediaRecorded.start(). Is /dev/null not a valid path on Android 11?
Logcat:
start failed: -1004
2020-11-15 10:51:41.827 11836-11836/= E/AndroidRuntime: FATAL EXCEPTION: main
Process: c=, PID: 11836
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
Replace "/dev/null" with the correct file path "${externalCacheDir.absolutePath}/test.3gp" and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With