android11(api30):在后台服务中录制音频不工作。
创始人
2024-08-20 09:00:59
0

在Android 11(API 30)中,Google采取了一些措施来限制应用程序后台使用资源,包括录音权限。这意味着在后台服务中录制音频可能无法正常工作。

要解决这个问题,可以使用MediaRecorder的新API,它允许您在前台服务中录制音频。

以下是示例代码:

//Start recording audio with MediaRecorder in a foreground service private void startRecording() { if (mMediaRecorder == null) { mMediaRecorder = new MediaRecorder(); }

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
    mMediaRecorder.setOutputFile(mFilePath);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mMediaRecorder.setAudioSamplingRate(44100);
    mMediaRecorder.setAudioEncodingBitRate(192000);

    try {
        mMediaRecorder.prepare();
        mMediaRecorder.start();
        startForeground(NOTIFICATION_ID, getNotification());
    } catch (IOException e) {
        Log.e(TAG, "startRecording: ", e);
    }
}

请注意,在使用foreground服务时,必须调用startForeground方法,否则应用程序将在一段时间后被系统强制关闭。

相关内容

热门资讯

透视规律!德普之星私人局辅助器... 透视规律!德普之星私人局辅助器(透视)德普之星app安卓版破解版-确实有挂(哔哩哔哩)德普之星app...
透视线上!aapoker公共底... 透视线上!aapoker公共底牌(透视)hhpoker是正品吗-切实是真的挂(哔哩哔哩)1、首先打开...
连日来!hhpoker万能辅助... 连日来!hhpoker万能辅助器(透视)德州局hhpoker-竟然是有挂(哔哩哔哩)1、hhpoke...
经调查!aapoker怎么设置... 经调查!aapoker怎么设置提高好牌几率(透视)hhpoker有辅助吗-一贯存在有挂(哔哩哔哩)a...
透视辅助!hhpoker透视码... 透视辅助!hhpoker透视码(透视)hhpoker辅助靠谱吗-原来真的有挂(哔哩哔哩)1、金币登录...
透视线上!德州局hhpoker... 透视线上!德州局hhpoker(透视)hhpoker真的有透视吗-确实真的是有挂(哔哩哔哩)1、用户...
透视模拟器!werplan辅助... 透视模拟器!werplan辅助软件(透视)佛手在线是不是有挂-果然是真的挂(哔哩哔哩)1、在佛手在线...
最终!aa poker辅助包(... 最终!aa poker辅助包(透视)xpoker辅助怎么用-都是存在有挂(哔哩哔哩)1、进入游戏-大...
黑科技辅助挂!aapoker怎... 黑科技辅助挂!aapoker怎么控制牌(透视)德州局hhpoker-竟然是真的挂(哔哩哔哩)1、进入...
据通报!wepoker养号规律... 据通报!wepoker养号规律(透视)wepoker私人局可以透视-一贯有挂(哔哩哔哩)1、wepo...