logo
当前页

屏幕共享


功能简介

屏幕共享,是指在视频通话或互动直播过程中,将屏幕内容以视频的方式分享给其他的观众,以增强互动体验,提高沟通效率。屏幕共享在如下场景中应用广泛:

  • 视频会议场景中,屏幕共享可以将发言者本地的文件、数据、网页、PPT 等画面分享给其他参会人。
  • 在线课堂场景中,屏幕共享可以将老师的课件、笔记、讲课内容等画面展示给学生观看。
开始屏幕共享移动应用共享的屏幕Web 应用共享的屏幕横屏模式

实现流程

共享屏幕

屏幕共享仅支持在宫格布局中使用,如需共享您的屏幕,您需要先将 ZegoUIKitPrebuiltLiveStreamingConfig 内的 layout 设置为 Gallery

如需在屏幕共享期间设置是否默认使用全屏模式,您需要在 ZegoLayoutGalleryConfig 内配置 showNewScreenSharingViewInFullscreenMode,将其设置为 true,即当开始屏幕共享时,共享的屏幕将自动全屏显示。

同时,全屏模式按钮是可自定义的,您可以通过配置 ZegoLayoutGalleryConfig 内的 showScreenSharingFullscreenModeToggleButtonRules,决定它的显示方式。

  • SHOW_WHEN_SCREEN_PRESSED:点击共享屏幕时,将显示全屏模式按钮。
  • ALWAYS_SHOW:始终显示全屏模式按钮。
  • ALWAYS_HIDE:始终隐藏全屏模式按钮。

如需开始屏幕共享,将 ZegoMenuBarButtonName.SCREEN_SHARING_TOGGLE_BUTTON 配置添加到 bottomMenuBarConfig 中,以便显示屏幕共享按钮。

以下是参考代码:

Java
Kotlin
public class LiveActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_live);

    long appID = YourAppID;
    String appSign = YourAppSign;
    String userID = "userID";
    String userName = "userName";
    String liveID = "testLiveID";

    // 修改您的自定义配置。
    ZegoUIKitPrebuiltLiveStreamingConfig config;
    if (isHost) {
        config = ZegoUIKitPrebuiltLiveStreamingConfig.host();
    } else {
        config = ZegoUIKitPrebuiltLiveStreamingConfig.audience();
    }
    config.bottomMenuBarConfig.hostButtons = Arrays.asList(ZegoMenuBarButtonName.TOGGLE_CAMERA_BUTTON,
       ZegoMenuBarButtonName.TOGGLE_MICROPHONE_BUTTON,ZegoMenuBarButtonName.SWITCH_CAMERA_FACING_BUTTON, ZegoMenuBarButtonName.SCREEN_SHARING_TOGGLE_BUTTON);

    ZegoLayoutGalleryConfig galleryConfig = new ZegoLayoutGalleryConfig();
    galleryConfig.removeViewWhenAudioVideoUnavailable = true;
    galleryConfig.showNewScreenSharingViewInFullscreenMode = true;
    galleryConfig.showScreenSharingFullscreenModeToggleButtonRules  = ZegoShowFullscreenModeToggleButtonRules.SHOW_WHEN_SCREEN_PRESSED;
    config.zegoLayout = new ZegoLayout(ZegoLayoutMode.GALLERY, galleryConfig);

    ZegoUIKitPrebuiltLiveStreamingFragment fragment = ZegoUIKitPrebuiltLiveStreamingFragment.newInstance(appID, appSign, userID, userName, liveID, config);

    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commitNow();
  }
}
1
Copied!
class LiveActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live)
        val appID: Long = YourAppID
        val appSign: String = YourAppSign
        val userID = "userID"
        val userName = "userName"
        val liveID = "testLiveID"

        // 修改您的自定义配置。
        val config: ZegoUIKitPrebuiltLiveStreamingConfig = if (isHost) {
            ZegoUIKitPrebuiltLiveStreamingConfig.host()
        } else {
            ZegoUIKitPrebuiltLiveStreamingConfig.audience()
        }
        config.bottomMenuBarConfig.hostButtons = Arrays.asList<ZegoMenuBarButtonName>(
            ZegoMenuBarButtonName.TOGGLE_CAMERA_BUTTON,
            ZegoMenuBarButtonName.TOGGLE_MICROPHONE_BUTTON,
            ZegoMenuBarButtonName.SWITCH_CAMERA_FACING_BUTTON,
            ZegoMenuBarButtonName.SCREEN_SHARING_TOGGLE_BUTTON
        )

        val galleryConfig = ZegoLayoutGalleryConfig()
        galleryConfig.removeViewWhenAudioVideoUnavailable = true
        galleryConfig.showNewScreenSharingViewInFullscreenMode = true
        galleryConfig.showScreenSharingFullscreenModeToggleButtonRules =
            ZegoShowFullscreenModeToggleButtonRules.SHOW_WHEN_SCREEN_PRESSED
        config.zegoLayout = ZegoLayout(ZegoLayoutMode.GALLERY, galleryConfig)
        val fragment = ZegoUIKitPrebuiltLiveStreamingFragment
            .newInstance(appID, appSign, userID, userName, liveID, config)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commitNow()
    }
}
1
Copied!

屏幕旋转

您可以参考以下流程,实现屏幕旋转:

  1. 在手机设置中,关闭旋转锁定功能。
  2. android:configChanges="locale|keyboardHidden|fontScale|orientation|screenSize|screenLayout|layoutDirection|density|uiMode" 属性添加到 Activitymanifest 文件配置中。以 LiveActivity 为例:
Untitled
<activity
   android:name=".LiveActivity"
   android:configChanges="locale|keyboardHidden|fontScale|orientation|screenSize|screenLayout|layoutDirection|density|uiMode"
   android:exported="false" />
1
Copied!

Previous

连麦时默认关闭摄像头

Next

使用 Token 鉴权