MediaRecorder Android 11 start failed -1004

我们两清 提交于 2021-02-08 15:18:36

问题


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)
 

回答1:


Replace "/dev/null" with the correct file path "${externalCacheDir.absolutePath}/test.3gp" and it should work.



来源:https://stackoverflow.com/questions/64846610/mediarecorder-android-11-start-failed-1004

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!