如何从Java在Android设备上运行adb screenrecord并结束屏幕记录? [英] How to can I run adb screenrecord on a Android Device from Java and end the screenrecording?

查看:1163
本文介绍了如何从Java在Android设备上运行adb screenrecord并结束屏幕记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Android 设备上运行 adb shell screenrecord 来自 Java 并结束 screenrecording



目前我必须指定 - 时间限制结束录制。如果我尝试在结束之前捕获视频,则失败。



有没有办法告诉adb停止录制?有没有办法配置线程将 CTRL-C 发送到 shell 命令?



这是在无根电话上运行。



代码

  videoExecutor.submit(() - > {
//开始录制视频
String recordVideo =screenrecord --time-limit + testRun.getVideoLength()++/ sdcard /+ logDate +。mp4;
try {
device.executeShell(recordVideo);
} catch(异常e1) {
e1.printStackTrace();
}
sleep(testRun.getVideoLength()* ONE_SECOND + TWO_SECONDS); //必须匹配--time-limit以上。

RemoteFile remote = new RemoteFile(/ sdcard /+ logDate +。mp4);
File local = new File(videoPath); //将文件保存到本地机器
if(! local.getParentFile()。exists()){
local.getParentFile()。mkdirs();
}

try {
//从手机复制视频到电脑
try {
device.pull(remote,local);
} catch(例外e){
e.printStackTrace();
}
} catch(异常e){
// TODO自动生成的catch块
e.printStackTrace();
}

});


解决方案

发送 SIGINT SIGHUP screenrecord 进程会导致它停止录制。



我刚刚在2个单独的 adb shell 会话中尝试了以下的无根(Android 6.0手机)电话:



in会话#1:

  screenrecord /sdcard/Movies/test.mpg 


  pkill -INT screenrecord 

并且 screenrecord 捕获在发出 pkill 命令


How to can I run adb shell screenrecord on a Android Device from Java and end the screenrecording?

Currently I have to specify the --time-limit to end the recording. If I try to capture the video prior to it ending it fails.

Is there a way to tell adb to stop recording? Is there a way to configure the thread to send a CTRL-C to the shell command?

This is to be run on unrooted phones.

Code

videoExecutor.submit(() -> {
          //Start recording video
        String recordVideo = "screenrecord --time-limit " + testRun.getVideoLength() + " " + "/sdcard/" + logDate + ".mp4";
    try {
      device.executeShell(recordVideo);
  } catch (Exception e1) {
    e1.printStackTrace();
  }
    sleep(testRun.getVideoLength()*ONE_SECOND + TWO_SECONDS);  //gotta match the --time-limit above.

RemoteFile remote = new RemoteFile("/sdcard/" + logDate + ".mp4");
File local = new File(videoPath); //Save file to local machine
          if(!local.getParentFile().exists()) {
              local.getParentFile().mkdirs();
          }

    try {
      //Copy video from phone to computer
        try {
                  device.pull(remote, local);
              } catch (Exception e) {
                  e.printStackTrace();
              }
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

      });

解决方案

Sending either SIGINT or SIGHUP to screenrecord process would cause it to stop recording.

I just tried the following on unrooted (stock Android 6.0) phone in 2 separate adb shell sessions:

in session #1:

screenrecord /sdcard/Movies/test.mpg

in session #2:

pkill -INT screenrecord

and the screenrecord capture stopped after issuing the pkill command

这篇关于如何从Java在Android设备上运行adb screenrecord并结束屏幕记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆