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;

相关内容

热门资讯

据监测!we-poker有人玩... 据监测!we-poker有人玩吗,卡五星辅助神器,总是存在有辅助app(哔哩哔哩)1、进入到卡五星辅...
辅助透视!智星德州辅助译码插件... 辅助透视!智星德州辅助译码插件靠谱吗,德州局透视脚本下载安装最新版本(透视)切实是真的有辅助教程(哔...
不少玩家反映!德州透视是真的吗... 不少玩家反映!德州透视是真的吗,逍遥湖北卡五星辅助器,一贯是真的有辅助脚本(哔哩哔哩)小薇(辅助器软...
详情透视!werplan脚本,... 详情透视!werplan脚本,pokemmo辅助器手机版下载(透视)都是一直都是有辅助神器(哔哩哔哩...
值得注意的是!werplan辅... 值得注意的是!werplan辅助软件,熊猫透视辅助软件,一贯是有辅助app(哔哩哔哩)1、上手简单,...
开挂透视!pokerworld... 开挂透视!pokerworld修改器,poker master辅助(透视)好像一直总是有辅助攻略(哔...
经调查!wpk控制牌是真的吗,... 经调查!wpk控制牌是真的吗,九哥玩挂,好像存在有辅助脚本(哔哩哔哩)1、玩家可以在九哥玩挂透视最简...
普及透视!德州私人局脚本,we... 普及透视!德州私人局脚本,wepoker透视有吗(透视)总是真的有辅助插件(哔哩哔哩);1、下载好德...
来临!wepoker破解工具,... 来临!wepoker破解工具,天天卡五星2辅助插件,总是真的是有辅助教程(哔哩哔哩)1、让任何用户在...
推荐透视!pokemmo脚本辅... 推荐透视!pokemmo脚本辅助下载,菠萝辅助器免费版的特点(透视)其实真的有辅助技巧(哔哩哔哩)1...