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插件!WPK神器透视挂a... WPK插件!WPK神器透视挂ai,wpk是真的还是假的,真是有ai透视挂操作1、每一步都需要思考,不...
WPK安卓!微扑克软件透视ai... WPK安卓!微扑克软件透视ai,pokerrrr2辅助,好像有ai透视攻略1、pokerrrr2辅助...
WPK软件!菠萝智星德州app... WPK软件!菠萝智星德州app透视ai,poker红龙辅助,本来真的有ai透视项目1、任何poker...
WPK控制牌!aapoker工... WPK控制牌!aapoker工具辅助器ai,wpk系统是否存在作弊行为,其实是有ai辅助器烘培1、任...
WPK辅助器!菠萝智星德州ap... WPK辅助器!菠萝智星德州app透视挂ai,微扑克游戏辅助器,真是真的是有ai透视挂操作1、游戏颠覆...
WPK脚本!德州扑克方法辅助a... 您好,哈糖大菠萝挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
WPK俱乐部!WePoKer攻... WPK俱乐部!WePoKer攻略开挂ai,werplan外挂,总是有ai开挂手段1、WPK俱乐部!W...
WPK购买!红龙扑克教程开挂a... WPK购买!红龙扑克教程开挂ai,哈糖大菠萝有挂吗5个常用方法,好像存在有ai开挂教材运哈糖大菠萝有...
WPK购买!微扑克教程透视ai... WPK购买!微扑克教程透视ai,AApoker辅助插件,总是有ai透视模块1、完成AApoker辅助...
WPK模拟器!菠萝智星德州工具... WPK模拟器!菠萝智星德州工具透视ai,AApoker中牌率,真是真的是有ai透视指南AApoker...