本文原创发布在华为开发者社区。
介绍
本示例基于C侧AVScreenCapture模块API实现视频录制功能,并将生成的录屏文件保存到到沙箱目录。
效果预览
使用说明
- 点击“start capture”按钮弹出是否允许应用使用屏幕的弹窗,点击允许开始屏幕录制。
- 点击“stop capture”按钮,屏幕录制停止,并生成录屏文件保存到沙箱目录。
实现思路
通过调用AVScreenCapture模块下的接口,实现屏幕录制的功能。先通过OH_AVScreenCapture_Create()创建一个capture实例,再初始化录屏参数,传入配置信息。通过OH_AVScreenCapture_SetMicrophoneEnabled()设置麦克风开关,当isMicrophone为true时,打开麦克风。调用OH_AVScreenCapture_StartScreenRecording()接口启动录屏,并保存录屏文件。核心代码如下,源码参考、
napi_init.cpp
// 初始化录屏参数,传入配置信息OH_AVScreenRecorderConfig
OH_RecorderInfo recorderInfo;
std::string fileUrl = "fd://" + std::to_string(value0);
recorderInfo.url = const_cast<char *>(fileUrl.c_str());
recorderInfo.fileFormat = OH_ContainerFormatType::CFT_MPEG_4;
config.recorderInfo = recorderInfo;
// 设置回调
OH_AVScreenCapture_SetStateCallback(capture, OnStateChange, nullptr);
// 进行初始化操作
int32_t retInit = OH_AVScreenCapture_Init(capture, config);
OH_LOG_Print(LOG_APP, LOG_INFO, 1, "test", "init:%{public}d", retInit);
// 开始录屏
int32_t micRet = OH_AVScreenCapture_SetMicrophoneEnabled(capture, true);
int32_t retStart = OH_AVScreenCapture_StartScreenRecording(capture);
OH_LOG_Print(LOG_APP, LOG_INFO, 1, "test", "start:%{public}d", retStart);
micRet = OH_AVScreenCapture_SetMicrophoneEnabled(capture, true);
napi_value sum;
napi_create_double(env, retStart, &sum);
return sum;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。