屏幕录制 - ScreenRecord
ScreenRecord 用于把屏幕录成视频文件(mp4)。与 Images.capture() 共用录屏/截图权限,需先通过 Access 授权。
前置权限
javascript
if (!Access.isMediaProjectionEnable()) {
Dialogs.confirm('温馨提示', '请开启屏幕录制权限', function (result) {
Access.openMediaProjectionSetting();
System.exit();
});
}
// 录屏过程中还要截图 / 找图时,建议同时开启无障碍
if (!Access.isAccessibilityServiceEnabled()) {
Dialogs.confirm('温馨提示', '请开启无障碍权限以便录屏中截图', function (result) {
Access.openAccessibilityServiceSetting();
System.exit();
});
}若 withAudio 为 true,还需系统已授予麦克风(RECORD_AUDIO)权限,可用 ScreenRecord.hasRecordAudioPermission() 检查。
输出路径
请写入 Files.getCachePath() 等应用可写目录,不要使用 /sdcard/Movies/ 等公共路径(Android 10+ 分区存储下容易启动失败)。
- 推荐 —
Files.getCachePath() + '/demo.mp4' project://前缀 — 相对项目根目录- 相对路径 — 默认按项目根目录解析
- 省略路径 — 写入应用缓存目录,文件名形如
screen_时间戳.mp4
start()
返回: {boolean} 是否启动成功
开始录屏,输出到缓存目录。
javascript
if (ScreenRecord.start()) {
console.log('开始录屏');
}start(outputPath)
参数: outputPath {string} 输出 mp4 路径
返回: {boolean} 是否启动成功
javascript
ScreenRecord.start(Files.getCachePath() + '/demo.mp4');start(outputPath, bitRate)
参数:
outputPath {string}输出路径bitRate {number}视频码率(bps),<=0时使用默认约6000000
返回: {boolean} 是否启动成功
javascript
ScreenRecord.start(Files.getCachePath() + '/demo.mp4', 8000000);start(outputPath, bitRate, frameRate)
参数:
outputPath {string}输出路径bitRate {number}码率(bps),<=0用默认frameRate {number}帧率,<=0时默认30
返回: {boolean} 是否启动成功
javascript
ScreenRecord.start(Files.getCachePath() + '/demo.mp4', 6000000, 30);start(outputPath, bitRate, frameRate, withAudio)
参数:
outputPath {string}输出路径bitRate {number}码率(bps)frameRate {number}帧率withAudio {boolean}是否录制麦克风声音
返回: {boolean} 是否启动成功
javascript
if (ScreenRecord.hasRecordAudioPermission()) {
ScreenRecord.start(Files.getCachePath() + '/demo.mp4', 6000000, 30, true);
} else {
ScreenRecord.start(Files.getCachePath() + '/demo.mp4');
}stop()
返回: {string|null} 输出文件路径;失败返回 null(例如录制过短)
停止录屏并返回文件路径。
javascript
let path = ScreenRecord.stop();
if (path) {
console.log('录屏完成:' + path);
// 录到缓存目录后,需要进相册时再调用:
// MediaStore.saveVideo(path);
}isRecording()
返回: {boolean} 是否正在录屏
javascript
if (ScreenRecord.isRecording()) {
console.log('录屏中…');
}getOutputPath()
返回: {string} 当前或最近一次录屏输出路径
javascript
console.log(ScreenRecord.getOutputPath());hasRecordAudioPermission()
返回: {boolean} 是否已授予麦克风权限
仅在需要 withAudio: true 时检查。
完整示例
javascript
if (!Access.isMediaProjectionEnable()) {
Dialogs.confirm('温馨提示', '请开启屏幕录制权限', function (result) {
Access.openMediaProjectionSetting();
System.exit();
});
}
let ok = ScreenRecord.start(Files.getCachePath() + '/deeke_demo.mp4', 6000000, 30, false);
if (!ok) {
console.log('启动录屏失败');
System.exit();
}
// 录屏过程中可同时截图 / 找图
for (let i = 0; i < 5; i++) {
let imageFile = Images.capture();
console.log('截图:' + imageFile);
System.sleep(2000);
}
let videoPath = ScreenRecord.stop();
if (videoPath) {
console.log('视频已保存:' + videoPath);
// 需要进相册时:
// MediaStore.saveVideo(videoPath);
} else {
console.log('停止录屏失败(可能录制过短)');
}注意
- 必须先开启录屏权限,否则
start返回false。 - 请把文件写到
Files.getCachePath()等应用可写目录,不要使用/sdcard/Movies/等公共路径。 - 录屏过程中仍可调用
Images.capture();若同时要截图/找图,建议开启无障碍。 - 录制时间过短时
stop()可能返回null,建议至少录几秒。 - 入库相册: 录到缓存目录后,需要出现在相册时再调用
MediaStore.saveVideo(path)。见 媒体文件 MediaStore。 - 相关:权限 Access、图色能力、音频播放 Audio。
