ume*_*ani 9 android mediarecorder javacv android-ffmpeg
我正在使用 MediaRecorder 和 MediaProjection Api 在 android 应用程序中录制屏幕。我无法获得可以通过 rtmp 流发送的每个帧。为了通过 RTMP 流发布,我使用 JAVACV Android 库。
例如 - 在通过相机直播的情况下,我们可以在 onPreviewFrame() 回调中获取每一帧。获取每一帧后,我只需使用 JAVACV 库的 FFmpegFrameRecorder 将每一帧流式传输到 rtmp url。
如何通过屏幕录制实现相同的效果?
任何帮助将不胜感激。提前致谢!
首先,MediaProjection 没有回调接口可以为您提供屏幕捕获帧的缓冲区,但是SurfaceTexture很有可能。这是使用 SurfaceTexture 进行屏幕捕获的一种实现ScreenCapturerAndroid。
VideoCapturer 的一种实现,用于将屏幕内容捕获为视频流。捕获是由 MediaProjection 在 SurfaceTexture 上完成的。我们使用 SurfaceTextureHelper 与这个 SurfaceTexture 交互。SurfaceTextureHelper 由本机代码创建并在 VideoCapturer.initialize() 中传递给此捕获器。在接收到新帧时,此捕获器将其作为纹理通过 CapturerObserver.onFrameCaptured() 传递给本机代码。这发生在给定 SurfaceTextureHelper 的 HandlerThread 上。处理完每一帧后,本机代码将缓冲区返回给 SurfaceTextureHelper
但是,如果您想发送应用程序视图的流,那么以下屏幕截图将为您提供帮助。
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);
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
用法
...
//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();
Run Code Online (Sandbox Code Playgroud)
使用它,您可以将视图内容发送到 RTMP 流,您可以使用活动的父视图来捕获活动的所有内容。
| 归档时间: |
|
| 查看次数: |
1571 次 |
| 最近记录: |