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;

相关内容

热门资讯

盘点一款!wepower德州设... 盘点一款!wepower德州设置,governorofpoker3有挂,详细教程(有挂介绍)-哔哩哔...
透明挂透视!(wePOke)软... 透明挂透视!(wePOke)软件透明挂,扑克王辅助工具,详细教程(有挂规律)-哔哩哔哩;wePOke...
玩家必看分享!(wepOkE)... 《wepOkE软件透明挂》是一款多人竞技的wepOkE辅助透视游戏,你将微扑克对手来到同一个战场,为...
专业讨论!德州扑克ai机器人,... 《专业讨论!德州扑克ai机器人,(wepOkE)软件透明挂,详细教程(有挂透明)-哔哩哔哩》 德州扑...
透视ai代打!德州ai辅助神器... 透视ai代打!德州ai辅助神器下载,pokermaster有外挂,详细教程(有挂技巧)-哔哩哔哩;玩...
玩家必看教程!poker有外挂... 玩家必看教程!poker有外挂,aa poker有外挂,详细教程(有挂规律)-哔哩哔哩;人气非常高,...
透明辅助!(wePOke)软件... 此外,数据分析德州(wePOke)辅助神器app还具备辅助透视行为开挂功能,通过对客户wePOke透...
总算了解!线上wpk到底有外挂... 总算了解!线上wpk到底有外挂,鱼扑克app ai,详细教程(有挂规律)-哔哩哔哩1、点击下载安装,...
必备攻略!轰趴大菠萝十三水有外... 必备攻略!轰趴大菠萝十三水有外挂,(wepoke)软件透明挂,详细教程(有挂介绍)-哔哩哔哩;原来确...
2024教程!(wepOke)... 2024教程!(wepOke)软件透明挂,wepower有辅助器,详细教程(有挂教学)-哔哩哔哩准备...