HarmonyOS鸿蒙Next中实现录屏功能示例代码
HarmonyOS鸿蒙Next中实现录屏功能示例代码
介绍
本示例基于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;
更多关于HarmonyOS鸿蒙Next中实现录屏功能示例代码的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中实现录屏功能,可以使用ScreenCapture类。以下是一个简单的示例代码:
import screenCapture from '@ohos.screenCapture';
// 创建录屏对象
let screenCaptureInstance = screenCapture.createScreenCapture();
// 设置录屏参数
let config = {
    width: 1080, // 录屏宽度
    height: 1920, // 录屏高度
    bitrate: 4000000, // 比特率
    frameRate: 30, // 帧率
    videoCodec: 'video/avc' // 视频编码格式
};
// 开始录屏
screenCaptureInstance.start(config).then(() => {
    console.log('录屏开始');
}).catch((err) => {
    console.error('录屏失败: ' + JSON.stringify(err));
});
// 停止录屏
screenCaptureInstance.stop().then(() => {
    console.log('录屏停止');
}).catch((err) => {
    console.error('停止录屏失败: ' + JSON.stringify(err));
});
这段代码首先创建了一个ScreenCapture对象,然后设置了录屏的参数,如宽度、高度、比特率、帧率和视频编码格式。接着调用start方法开始录屏,调用stop方法停止录屏。录屏过程中,可以通过Promise对象来处理成功或失败的回调。
更多关于HarmonyOS鸿蒙Next中实现录屏功能示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中实现录屏功能,可以使用ScreenRecorder类。以下是一个简单的录屏示例代码:
import ohos.media.screenrecorder.ScreenRecorder;
import ohos.media.screenrecorder.ScreenRecorderConfig;
public class ScreenRecordExample {
    private ScreenRecorder screenRecorder;
    public void startRecording() {
        ScreenRecorderConfig config = new ScreenRecorderConfig.Builder()
                .setVideoSource(ScreenRecorderConfig.VideoSource.SCREEN)
                .setAudioSource(ScreenRecorderConfig.AudioSource.MIC)
                .setOutputPath("/sdcard/record.mp4")
                .build();
        screenRecorder = new ScreenRecorder(config);
        screenRecorder.start();
    }
    public void stopRecording() {
        if (screenRecorder != null) {
            screenRecorder.stop();
            screenRecorder.release();
        }
    }
}
这段代码展示了如何配置和启动屏幕录制。startRecording方法开始录制,stopRecording方法停止并释放资源。录制的视频将保存到指定路径。
        
      
                  
                  
                  
