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、每一步都需要思考,...
德州ai人工智能!德州透视辅助... 德州ai人工智能!德州透视辅助工具,gg扑克正规(详细透视辅助插件教程);1、进入到gg扑克正规黑科...
德扑ai智能!德扑之星实战,德... 德扑ai智能!德扑之星实战,德扑好像真的有挂(详细透视辅助脚本教程)小薇(透视辅助)致您一封信;亲爱...
德州之星有外 挂!德州专用扑克... 德州之星有外 挂!德州专用扑克高科技,fishpoker app辅助(详细透视辅助插件解密)亲,关键...
德扑之星猫腻!德扑软件高端,德... 德扑之星猫腻!德扑软件高端,德扑平台原来真的有挂(详细透视辅助软件教程)1、上手简单,内置详细流程视...
德州ai辅助!GG扑克辅助软件... 德州ai辅助!GG扑克辅助软件,云扑克辅助器真假(详细透视辅助脚本黑科技)1、操作简单,无需注册,只...
德扑之星猫腻!德扑软件开发,德... 德扑之星猫腻!德扑软件开发,德扑平台确实真的有挂(详细透视辅助工具教程)1、很好的工具软件,可以解锁...
德州之星插件!德州之星软件,云... 德州之星插件!德州之星软件,云扑克有挂(详细透视辅助插件技巧);1、在云扑克有挂ai机器人技巧中,中...
德扑之星猫腻!德扑ai代打会检... 德扑之星猫腻!德扑ai代打会检测到,德扑之星其实真的有挂(详细透视辅助app教程)1、进入游戏-大厅...
德州ai辅助软件!德州ai机器... 德州ai辅助软件!德州ai机器人软件开发,来玩德州app有挂(详细透视辅助神器细节)1、每一步都需要...