首页 > 解决方案 > Android Screen Share - 表面参数

问题描述

我正在尝试使用 Agora.io 在 Android 上实现屏幕共享。他们的样本很清楚:

MediaProjectionManager projectManager = (MediaProjectionManager) mContext.getSystemService(
Context.MEDIA_PROJECTION_SERVICE);

// Create the intent for screen capture. Call the startActivityForResult method to use the sharing function.
Intent intent = projectManager.createScreenCaptureIntent();
startActivityForResult(intent);

MediaProjection projection;
VirtualDisplay display;

// Override and implement the onActivityResult method of the Activity where you just called startActivityForResult.
@Override
onActivityResult(int requestCode, int resultCode, Intent resuleData) {
    projection = projectManager.getMediaProjection(resultCode, resultData);
    display = projection.createVirtualDisplay(name, width, height, dpi, flags, surface, callback, handler);
}

// The texture retrieved from the Surface will be sent by the SDK.
rtcEngine.pushExternalVideoFrame(new AgoraVideoFrame(...));

// Stop screen sharing.
projection.stop();

然而在createVirtualDisplay它们是一个表面参数。我不确定它来自哪里 - 或者更多我如何获得屏幕表面?还是我要实例化一个新的 Surface 实例?使用接受 SurfaceTexture 作为参数的构造函数。或者可能实现 SurfaceTexture.OnFrameAvailableListener / OnImageAvailableListener (不确定)。

表面文档:

https://developer.android.com/reference/android/view/Surface

createVirtualDisplay 文档:

https://developer.android.com/reference/android/media/projection/MediaProjection#createVirtualDisplay(java.lang.String,%20int,%20int,%20int,%20int,%20android.view.Surface,%20android。 hardware.display.VirtualDisplay.Callback,%20android.os.Handler)

标签: androidscreenagora.io

解决方案


您可以通过多种方式获取 Surface:

  1. 如果你正在做一些视频编码,使用MediaCodec.createInputSurface()作为编码模块的输入;
  2. 如果要接收 YUV 帧,请使用ImageReader.getSurface()创建 Surface ;
  3. 如果你想自己做 OpenGL 渲染,你可以在 OpenGL 上下文下创建一个纹理,并使用该纹理创建一个 SurfaceTexture,从而创建你自己的 Surface。

但请注意,不要使用已附加到视图层次结构的 Surface。


推荐阅读