Android 录屏录制功能:require a foreground service of type ServiceInfo.FOREGROUND_SERVICE

28 篇文章 0 订阅

android 10.0录制屏幕报错. Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE

报错原因

  1. Android10.0以上的录制屏幕需要获取到FOREGROUND_SERVICE权限
  2. Android10.0以上实例化mediaProjection需要在service里进行
  3. Android10.0以上录制屏幕需要添加notification,提醒用户该app正在录制屏幕

Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

报错解决 (这里默认你已经写好了正常的录制屏幕流程)

1.在AndroidManifest.xml内的service添加foregroundServiceType

android:foregroundServiceType="mediaProjection"


2,在申请录屏幕权限后的返回数据onActivityResult内对sdk进行判断,并实现startForegroundService。

这样我们就可以在service内进行初始化mediaProjection了

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                screenRecordService.setMediaProject(MediaProject); //将MediaProject传进service里,这个medieaProject应该为 null
                screenRecordService.setMediaProjectionManager(MediaProjectionManager); //将mediaProjectionManager传进service里,这个manager你应该已经在`ServiceConnection`时实例化好了()
             
                Intent service = new Intent(this, ScreenRecordService.class);
                service.putExtra("code", resultCode);
                service.putExtra("data", data);
                startForegroundService(service);
            }
        }

调过startForegroundService会执行service的 onStartCommand 方法,之后我们就可以在service里进行实例化 MediaProject了。

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel(); //创建通知栏,你正在录屏
            Bundle bundle = intent.getExtras();
            MediaProject = mediaProjectionManager.getMediaProjection( bundle.getInt("code",-1), Objects.requireNonNull(intent.getParcelableExtra("data")));
            if(null != mOnStartCommandListener){
                mOnStartCommandListener.finished(mediaProjection!=null);
            }
        }        return START_STICKY;
    }

3.构造Notification

    private void createNotificationChannel() {
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        Intent nfIntent = new Intent(this, TutorW2Activity.class); //点击后跳转的界面,可以设置跳转数据

        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
                //.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setContentText("is running......") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

        /*以下是对Android 8.0的适配*/
        //普通notification适配
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("notification_id");
        }
        //前台服务notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(110, notification);

    }


————————————————

原文链接:https://blog.csdn.net/ruiruiddd/article/details/117952037

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要同时录制音频和屏幕,可以借助AndroidMediaProjectionMediaRecorder类。MediaProjection类可以用来获取屏幕的图像数据,MediaRecorder类可以用来录制音频和视频。 以下是实现此功能的基本步骤: 1. 获取MediaProjection实例 使用MediaProjectionManager类获取MediaProjection实例,代码如下: ``` MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent(); startActivityForResult(permissionIntent, REQUEST_CODE); ``` 在onActivityResult()方法中获取MediaProjection实例: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data); } } ``` 2. 配置MediaRecorder 设置MediaRecorder的音频和视频源,以及输出格式、输出路径等参数,代码如下: ``` mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setOutputFile(outputPath); mediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setVideoEncodingBitRate(512 * 1000); mediaRecorder.setVideoFrameRate(30); ``` 3. 创建VirtualDisplay 使用MediaProjection.createVirtualDisplay()方法创建VirtualDisplay实例,代码如下: ``` virtualDisplay = mediaProjection.createVirtualDisplay("ScreenCapture", DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mediaRecorder.getSurface(), null, null); ``` 4. 开始录制 调用MediaRecorder.start()方法开始录制,代码如下: ``` mediaRecorder.prepare(); mediaRecorder.start(); ``` 5. 停止录制 调用MediaRecorder.stop()方法停止录制,代码如下: ``` mediaRecorder.stop(); mediaRecorder.reset(); virtualDisplay.release(); ``` 注意事项: 1. 在Android 6.0及以上版本,需要在运行时请求录音和屏幕录制权限。 2. 在录制音频时,需要获取录音权限,并且需要处理录音数据的回调。 3. 在录制视频时,需要处理视频帧的回调。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值