从API20开始,屏幕录制支持矩形区域录制是在现有的录制区域捕获基础上开放的能力,允许开发者自主选择录屏需要捕获的区域位置,并通过调整屏幕捕获ID和指定的捕获区域area确定矩形录制区域。
在功能开发前,开发者需要掌握以下基础概念:
用户希望能够提供矩形区域录制功能的场景,例如:
在CMake脚本中链接动态库
target_link_libraries(sample PUBLIC libnative_avscreen_capture.so libnative_display_manager.so libability_runtime.so)
上述'sample'字样仅为示例,此处由开发者根据实际工程目录自定义。
添加头文件
#include "hilog/log.h" #include "napi/native_api.h" #include <window_manager/oh_display_info.h> #include <window_manager/oh_display_manager.h> #include <AbilityKit/ability_runtime/application_context.h> #include <multimedia/player_framework/native_avscreen_capture.h> #include <multimedia/player_framework/native_avscreen_capture_base.h> #include <multimedia/player_framework/native_avscreen_capture_errors.h> #include <unistd.h> #include <fcntl.h> #include <string>
调用OH_AVScreenCapture_SetCaptureArea()接口传入希望录制的矩形区域。
示例中的变量说明如下:
录制区域限制:
不支持跨屏(一边在左边/一边在右边)录制。
录制区域更改:支持录制过程中,更新录制区域。
设置失败的处理:如果区域位置设置失败,系统将按照上一次的区域进行捕获。建议开发者在设置区域时进行错误检查和处理,以确保捕获区域的准确性。
参数设置非负:该接口设置的坐标和宽高不能为负数。
g_avCapture = OH_AVScreenCapture_Create();
if (g_avCapture == nullptr) {
OH_LOG_ERROR(LOG_APP, "create screen capture failed");
}
OpenFile("Demo");
SetCallback(g_avCapture);
// 初始化录屏,传入配置信息OH_AVScreenCaptureConfig。
OH_AVScreenCaptureConfig config;
SetConfig05(config);
OH_AVSCREEN_CAPTURE_ErrCode result = OH_AVScreenCapture_Init(g_avCapture, config);
if (result != AV_SCREEN_CAPTURE_ERR_OK) {
OH_LOG_ERROR(LOG_APP,
"==ScreenCaptureSample== ScreenCapture OH_AVScreenCapture_Init failed %{public}d", result);
}
// 1. 可选,可以根据需要设置区域坐标和大小,设置想要捕获的区域,如下方创建了一个从(0, 0)为起点的长100,宽100的矩形区域。
OH_Rect* region = new OH_Rect;
region->x = 0;
region->y = 0;
region->width = CAPTURE_REGION_SIZE;
region->height = CAPTURE_REGION_SIZE;
// 2.传入矩形区域所在的屏幕ID。
uint64_t regionDisplayId = 0;
OH_AVScreenCapture_SetCaptureArea(g_avCapture, regionDisplayId, region);
// 开始录屏。
result = OH_AVScreenCapture_StartScreenCapture(g_avCapture);
delete region;
region = nullptr;