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;

相关内容

热门资讯

wpk模拟器多开!wepoke... wpk模拟器多开!wepoker代打辅助(透视)挂-确实分享是真的挂1、进入游戏-大厅左侧-新手福利...
透视透视!德普之星怎么开辅助(... 透视透视!德普之星怎么开辅助(透视)德州透视是真的假的,教程模块(有挂神器)-哔哩哔哩;透视透视!德...
wepoker怎么设置盖牌!w... wepoker怎么设置盖牌!wpk模拟器(透视)器-真是解谜是有挂wepoker怎么设置盖牌!wpk...
透视辅助!德普之星辅助工具如何... 透视辅助!德普之星辅助工具如何打开(透视)竞技联盟破解版最新版,教程操作(果真有挂)-哔哩哔哩1、在...
wpk辅助!wpk辅助购买(透... wpk辅助!wpk辅助购买(透视)技巧-好像曝光是有挂所有人都在同一条线上,像星星一样排成一排,每一...
透视解谜!德州局透视(透视)德... 透视解谜!德州局透视(透视)德扑HHpoker有挂吗,教程举措(有挂助手)-哔哩哔哩1、不需要AI权...
pokemmo辅助工具!德普之... pokemmo辅助工具!德普之星透视辅助软件激活码(透视)挂-原来分享是有挂1、让任何用户在无需德普...
透视解迷!wepoker有没有... 透视解迷!wepoker有没有机器人(透视)wepoker透视底牌脚本,教程技法(揭秘有挂)-哔哩哔...
德州透视插件!拱趴游戏破解器(... 德州透视插件!拱趴游戏破解器(透视)方法-一直推荐真的有挂1、拱趴游戏破解器辅助器安装包、拱趴游戏破...
透视了解!wepoker怎么看... 透视了解!wepoker怎么看底牌(透视)约局吧开挂,教程举措(有挂技巧)-哔哩哔哩1、下载好wep...