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

北战南征 提交于 2019-12-18 07:08:23

问题


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();
  }

      });

回答1:


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



来源:https://stackoverflow.com/questions/38363680/how-to-can-i-run-adb-screenrecord-on-a-android-device-from-java-and-end-the-scre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!