Android11停止在前台服务中录制音频,如果应用程序进入后台或被杀死。
创始人
2024-08-20 12:00:07
0

在Android 11中,应用程序在后台运行时无法使用前台服务中的录音功能。解决此问题的一种方法是使用MediaRecorder API并执行后台录音。以下是示例代码:

1.添加适当的权限到AndroidManifest.xml文件中:

2.在服务(Service)类中实例化MediaRecorder并设置相应的录音属性:

private MediaRecorder recorder; private static String outputFile = null;

public void startRecording() { recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); recorder.setOutputFile(getOutputFile()); try { recorder.prepare(); } catch (IOException e) { Log.e("TAG", "prepare() failed"); } recorder.start(); } public void stopRecording() { recorder.stop(); recorder.release(); recorder = null; }

private String getOutputFile() { if (outputFile == null) { File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/"); if (!dir.exists()) { dir.mkdirs(); } outputFile = dir.getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4"; } return outputFile; }

3.在服务类中添加以下代码:

private NotificationManager notificationManager;

@Override public void onCreate() { super.onCreate(); notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); }

@Override public int onStartCommand(Intent intent, int flags, int startId) { Notification notification = new Notification.Builder(this, YOUR_CHANNEL_ID) .setContentTitle("My Recording Service") .setContentText("Recording...") .setSmallIcon(R.drawable.ic_launcher) .build(); startForeground(1, notification); startRecording(); return START_STICKY;

相关内容

热门资讯

最新消息!智星菠萝辅助,德普辅... 最新消息!智星菠萝辅助,德普辅助软件,真是是真的有挂(真是有挂)1、每一步都需要思考,不同水平的挑战...
如何分辨真伪!hardrock... 如何分辨真伪!hardrock透视工具,hh poker透视器下载,原来真的是有挂(果真有挂)1、完...
于此同时!德州局透视脚本免费版... 于此同时!德州局透视脚本免费版下载手机版,德扑软件,原来真的是有挂(有挂功能)在进入德扑软件软件靠谱...
今日公布!aapoker破解侠... 今日公布!aapoker破解侠是真的吗,微扑克游戏辅助器,原来真的是有挂(有挂助手)1、进入游戏-大...
攻略辅助挂!wpk有辅助器吗,... 攻略辅助挂!wpk有辅助器吗,德普之星透视辅助软件是真的吗,一贯真的是有挂(竟然有挂)1)德普之星透...
传递经验!wepoker辅助是... 传递经验!wepoker辅助是真的假的,wepoker免费透视脚本,一直有挂(果真有挂)1、wepo...
经核实!哈糖大菠萝开挂,德普之... 经核实!哈糖大菠萝开挂,德普之星透视辅助,其实存在有挂(有挂教程)1、不需要AI权限,帮助你快速的进...
玩家必看科普!pokemmo手... 玩家必看科普!pokemmo手机版脚本免费,wpk模拟器是什么,总是是有挂(有挂猫腻)玩家必看科普!...
第三方技巧!hhpoker破解... 第三方技巧!hhpoker破解工具,德普辅助软件,原来真的有挂(有挂秘笈)1、打开软件启动之后找到中...
总算了解!hhpkoer辅助挂... 总算了解!hhpkoer辅助挂是真的吗,wepoker有机器人吗,其实是真的有挂(有挂攻略)1、完成...