首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android屏幕录音-我如何才能得到每一帧?

在Android屏幕录音-我如何才能得到每一帧?
EN

Stack Overflow用户
提问于 2020-01-21 06:41:08
回答 1查看 2.4K关注 0票数 9

我正在使用MediaRecorder和MediaProjection Api在安卓应用程序中录制屏幕。我无法获得每个帧,我可以发送一个rtmp流。对于在RTMP流上发布,我使用JAVACV库。

对于例如-在通过摄像头直播的情况下,我们可以在onPreviewFrame()回调中获得每个帧。在获取每个帧之后,我只需使用JAVACV库的FFmpegFrameRecorder将每个帧流到rtmp url。

如何通过屏幕记录实现同样的目标?

如果能提供任何帮助,我们将不胜感激。提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2020-01-30 09:57:19

首先,MediaProjection没有回调接口,可以为屏幕捕获帧提供缓冲区,但使用SurfaceTexture是很有可能的。下面是用ScreenCapturerAndroid实现屏幕捕获的一个实现。

一种VideoCapturer的实现,用于将屏幕内容捕获为视频流。捕获是由MediaProjection在SurfaceTexture上完成的。我们使用一个SurfaceTexture与SurfaceTextureHelper进行交互。SurfaceTextureHelper由本机代码创建,并在VideoCapturer.initialize()中传递给这个捕获器。在接收到新帧时,该捕获器通过CapturerObserver.onFrameCaptured()将其作为纹理传递给本机代码。这发生在给定SurfaceTextureHelper的SurfaceTextureHelper上。当处理每个帧时,本机代码将缓冲区返回给SurfaceTextureHelper。

但是如果您想发送应用程序视图的流,那么下面的屏幕捕获器将帮助您。

代码语言:javascript
运行
复制
public class ScreenCapturer {
    private boolean capturing = false;
    private int fps=15;
    private int width,height;
    private Context context;
    private int[] frame;
    private Bitmap bmp;
    private Canvas canvas;
    private View contentView;
    private Handler mHandler = new Handler();
    public ScreenCapturer(Context context, View view) {
        this.context = context;
        this.contentView = view;
    }

    public void startCapturing(){
        capturing = true;

        mHandler.postDelayed(newFrame, 1000 / fps);
    }
    public void stopCapturing(){
        capturing = false;
        mHandler.removeCallbacks(newFrame);
    }
    private Runnable newFrame = new Runnable() {
        @Override
        public void run() {
            if (capturing) {
                int width = contentView.getWidth();
                int height = contentView.getHeight();
                if (frame == null ||
                        ScreenCapturer. this.width != width ||
                        ScreenCapturer.this.height != height) {

                    ScreenCapturer.this.width = width;
                    ScreenCapturer.this.height = height;

                    if (bmp != null) {
                        bmp.recycle();
                        bmp = null;
                    }
                    bmp = Bitmap.createBitmap(width,
                            height, Bitmap.Config.ARGB_8888);

                    canvas = new Canvas(bmp);
                    frame = new int[width * height];
                }
                canvas.saveLayer(0, 0, width, height, null);
                canvas.translate(-contentView.getScrollX(), - contentView.getScrollY());
                contentView.draw(canvas);

                bmp.getPixels(frame, 0, width, 0, 0, width, height);


               //frame[] is a rgb pixel array compress it to YUV if want and send over RTMP

                canvas.restore();
                mHandler.postDelayed(newFrame, 1000 / fps);

            }
        }
    };


}

使用

代码语言:javascript
运行
复制
...
//Use this in your activity 

private View parentView;

parentView = findViewById(R.id.parentView);
capturer = new ScreenCapturer(this,parentView);

//To start capturing
 capturer.startCapturing();


//To Stop capturer
capturer.stopCapturing();

使用它,您可以将视图内容发送到RTMP流,您可以使用活动的父视图来捕获活动的所有内容。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59835472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档