autojs实现无root录屏
程序员文章站
2024-03-14 11:32:58
...
效果
//本脚本由autojs交流群553908361收集整理
"ui";
importClass(android.content.Context);
importClass(android.media.MediaRecorder);
importClass(java.io.File);
importClass(java.lang.System);
importClass(android.os.Environment);
importClass(android.hardware.display.DisplayManager);
/*
¥¥无名小姐 制作
*/
runtime.requestPermissions(["RECORD_AUDIO"]);
//
running = false;
width = 720;
height = 1080;
dpi = 1;
mediaRecorder = new MediaRecorder();
ui.layout(
<vertical>
<appbar>
<toolbar title="无root录屏"/>
</appbar>
<Switch id="autoService" text="无障碍服务" checked="{{auto.service != null}}" padding="8 8 8 8" textSize="15sp"/>
<frame height="200" gravity="center">
<text text="脚本群群员专用 群553908361.结束运行后,保存在ScreenRecord目录" gravity="center"/>
</frame>
<button text="开始录屏"style="Widget.AppCompat.Button.Colored" id="button"/>
</vertical>
);
ui.button.click(function() {
if (running) {
stopRecord();
ui.button.setText("开始录屏");
} else {
startintent();
ui.button.setText("停止录屏");
}
});
ui.autoService.on("check", function(checked) {
// 用户勾选无障碍服务的选项时,跳转到页面让用户去开启
if(checked && auto.service == null) {
app.startActivity({
action: "android.settings.ACCESSIBILITY_SETTINGS"
});
}
if(!checked && auto.service != null){
auto.service.disableSelf();
}
});
// 当用户回到本界面时,resume事件会被触发
ui.emitter.on("resume", function() {
// 此时根据无障碍服务的开启情况,同步开关的状态
ui.autoService.checked = auto.service != null;
});
ui.emitter.on("activity_result", (requestCode, resultCode, data) => {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
if (mediaProjection) {
startRecord();
}
});
events.on("exit", function() {
if (running) {
stopRecord();
}
toastLog("结束运行,保存在ScreenRecord目录");
});
function createVirtualDisplay() {
virtualDisplay = mediaProjection.createVirtualDisplay(
"无名小姐",
width,
height,
dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mediaRecorder.getSurface(),
null, null);
}
function initRecorder() {
file = new File(getsaveDirectory(), System.currentTimeMillis() + ".mp4");
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(file.getAbsolutePath());
mediaRecorder.setVideoSize(width, height);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
mediaRecorder.setVideoFrameRate(30);
try {
mediaRecorder.prepare();
} catch (e) {
log(e);
}
}
function startintent() {
SCREEN_CAPTURE_REQUEST_CODE = 10086;
mediaProjectionManager = context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
intent = mediaProjectionManager.createScreenCaptureIntent();
activity.startActivityForResult(intent, SCREEN_CAPTURE_REQUEST_CODE);
}
function startRecord() {
if (mediaProjection == null || running) {
return false;
}
initRecorder();
createVirtualDisplay();
mediaRecorder.start();
running = true;
return true;
}
function stopRecord() {
if (!running) {
return false;
}
running = false;
mediaRecorder.stop();
mediaRecorder.reset();
virtualDisplay.release();
mediaProjection.stop();
return true;
}
function getsaveDirectory() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "ScreenRecord" + "/";
file = new File(rootDir);
if (!file.exists()) {
if (!file.mkdirs()) {
return null;
}
}
toastLog(rootDir);
return rootDir;
} else {
return null;
}
}
上一篇: Faster-RCNN检测-RPN