问题描述
在Android 11上,我的MediaRecorder无法初始化.我怀疑问题与范围存储有关,但我一直无法找出原因.我正在使用MediaRecorder来录制来自麦克风的音频.我从音频中提取振幅,所以我无意保留文件,这就是为什么路径为/dev/null
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))
}
崩溃发生在MediaRecorded.start(). /dev/null在Android 11上不是有效路径吗?
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)
推荐答案
替换"/dev/null";具有正确的文件路径"$ {externalCacheDir.absolutePath}/test.3gp";而且应该可以.
Replace "/dev/null" with the correct file path "${externalCacheDir.absolutePath}/test.3gp" and it should work.
这篇关于MediaRecorder Android 11启动失败-1004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!