BRTC SDK 在 Android 系统上的屏幕分享功能仅支持全屏幕,不能只共享某个 App 或者屏幕的局部。您可以选择在屏幕共享开始后,使用屏幕画面自动替换当前摄像头画面(如有),也可以选择保留摄像头画面推流,再增加一路屏幕共享流(辅流)。
⚠️使用屏幕共享功能,需要 App 申请若干权限(如使用悬浮窗),并添加前台服务代码(高版本 Android 必需),以确保 App 退到后台仍然可以继续分享屏幕。
当您调用 startScreenCapture 时,可以指定屏幕分享的编码质量。其中关于参数的设置,有几点需要说明:
当推流端使用 startScreenCapture 接口开启屏幕分享后,存在以下一些情况:
BRTCVideoStreamTypeBig
BRTCVideoStreamTypeSub
从 Android 7.0 系统开始,切入到后台运行的普通 App 进程,但凡有 CPU 活动,都很容易会被系统强杀掉。 所以当 App 在切入到后台默默进行屏幕分享时,通过弹出悬浮窗的方案,可以避免被系统强杀掉。 同时,在手机屏幕上显示悬浮窗也有利于告知用户当前正在做屏幕分享,避免用户泄漏个人隐私。
以下是通过 Android 系统的 WindowManager 在屏幕上添加一个悬浮视图(悬浮窗)的示例代码:
private WindowManager mWindowManager; private WindowManager.LayoutParams mLayoutParams; public void showView(View view, int width, int height) { mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); // TYPE_TOAST仅适用于4.4+系统,假如要支持更低版本使用TYPE_SYSTEM_ALERT(需要在manifest中声明权限) // 7.1(包含)及以上系统对TYPE_TOAST做了限制 int type = WindowManager.LayoutParams.TYPE_TOAST; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { type = WindowManager.LayoutParams.TYPE_PHONE; } else { type = WindowManager.LayoutParams.TYPE_TOAST; } mLayoutParams = new WindowManager.LayoutParams(type); mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mLayoutParams.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; mLayoutParams.width = width; mLayoutParams.height = height; mLayoutParams.format = PixelFormat.TRANSLUCENT; mWindowManager.addView(view, mLayoutParams); }
例如:
public class ScreenShareService extends Service { private static final int NOTIFICATION_ID = 888; private static final String CHANNEL_ID = "XXX_SCREEN_SHARE"; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(NOTIFICATION_ID, createNotification()); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); stopForeground(true); } private Notification createNotification() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "ScreenShareService", NotificationManager.IMPORTANCE_HIGH); channel.setDescription("Service for screen capture"); channel.enableVibration(false); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("Your App Name"); builder.setContentText("This is screen share service"); builder.setContentIntent(null); builder.setWhen(System.currentTimeMillis()); return builder.build(); } }
然后在您的 App 的 AndroidManifest.xml 文件中声明:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" /> <application> <service android:name="com.xxx.xxx.ScreenShareService" android:enabled="false" android:foregroundServiceType="mediaProjection"/> </application>
屏幕分享
BRTC SDK 在 Android 系统上的屏幕分享功能仅支持全屏幕,不能只共享某个 App 或者屏幕的局部。
您可以选择在屏幕共享开始后,使用屏幕画面自动替换当前摄像头画面(如有),也可以选择保留摄像头画面推流,再增加一路屏幕共享流(辅流)。
⚠️使用屏幕共享功能,需要 App 申请若干权限(如使用悬浮窗),并添加前台服务代码(高版本 Android 必需),以确保 App 退到后台仍然可以继续分享屏幕。
启动屏幕分享
设定编码参数
当您调用 startScreenCapture 时,可以指定屏幕分享的编码质量。其中关于参数的设置,有几点需要说明:
观看屏幕分享
当推流端使用 startScreenCapture 接口开启屏幕分享后,存在以下一些情况:
BRTCVideoStreamTypeBig:BRTCVideoStreamTypeSub:弹出悬浮窗以避免被强杀
从 Android 7.0 系统开始,切入到后台运行的普通 App 进程,但凡有 CPU 活动,都很容易会被系统强杀掉。 所以当 App 在切入到后台默默进行屏幕分享时,通过弹出悬浮窗的方案,可以避免被系统强杀掉。 同时,在手机屏幕上显示悬浮窗也有利于告知用户当前正在做屏幕分享,避免用户泄漏个人隐私。
以下是通过 Android 系统的 WindowManager 在屏幕上添加一个悬浮视图(悬浮窗)的示例代码:
添加前台服务
例如:
然后在您的 App 的 AndroidManifest.xml 文件中声明: