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;

相关内容

热门资讯

透视ai!wepoker破解器... 透视ai!wepoker破解器有用吗(透视)总是是有辅助app(有挂方针)-哔哩哔哩1)wepoke...
据悉!模拟器打开hhpoker... 据悉!模拟器打开hhpoker(透视)竟然有辅助app(今日头条)-哔哩哔哩1、模拟器打开hhpok...
这一问题亟待解决!佛手在线大菠... 这一问题亟待解决!佛手在线大菠萝技巧(透视)原来有辅助平台(有挂功能)-哔哩哔哩1、任何佛手在线大菠...
透视模拟器!wepoker免费... 透视模拟器!wepoker免费脚本咨询(透视)都是是真的辅助修改器(讲解有挂)-哔哩哔哩1、wepo...
透视新版!德州透视是真的吗(透... 透视新版!德州透视是真的吗(透视)竟然有辅助软件(有挂细节)-哔哩哔哩暗藏猫腻,小编详细说明德州透视...
有了最新消息!wepoker怎... 有了最新消息!wepoker怎么设置房间(透视)切实是有辅助辅助器(有挂秘笈)-哔哩哔哩1、wepo...
透视ai代打!pokemmo手... 透视ai代打!pokemmo手机脚本辅助器(透视)一直是有辅助下载(有挂细节)-哔哩哔哩1、在pok...
记者获悉!wepoker一直输... 记者获悉!wepoker一直输的号能继续打吗(透视)其实真的有辅助安装(有挂方法)-哔哩哔哩暗藏猫腻...
透视规律!hhpoker辅助靠... 透视规律!hhpoker辅助靠谱吗(透视)果然真的有辅助工具(有挂方法)-哔哩哔哩1、实时hhpok...
透视透视!wepoker底牌透... 透视透视!wepoker底牌透视脚本(透视)一贯真的是有辅助修改器(有挂分享)-哔哩哔哩暗藏猫腻,小...