如何在Java中运行adb screenrecord并结束屏幕录制?

3

如何在Java中运行adb shell screenrecord并结束screenrecording?涉及到Android设备和IT技术相关内容。

目前我必须指定--time-limit来结束录制。如果在录制结束之前尝试捕获视频,则会失败。

有没有办法告诉adb停止录制?是否可以配置线程向shell命令发送CTRL-C

这是要在未root的手机上运行。

代码:

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个回答

5
发送SIGINTSIGHUP信号给screenrecord进程会导致其停止录制。

我在未root的(Android 6.0原版)手机上尝试了以下内容,在2个独立的adb shell会话中:

在会话#1中:

screenrecord /sdcard/Movies/test.mpg

在第二次会议中:

pkill -INT screenrecord

在执行 pkill 命令后,screenrecord 的录制停止了。


这些手机没有被root,所以看起来它们没有访问killall的权限。adb shell killall -q =1 screenrecord /system/bin/sh: killall: not found - ALM
嗨,Alex,你是指在服务器上终止进程吗?我想要在手机上终止它。看起来未解锁的 Android 手机没有访问 kill 或 killall 的权限,除非我漏掉了什么?谢谢/ system / bin / sh:kill:998:操作不允许 - ALM
请仔细检查您的pid - 当尝试向在相同用户ID下运行的进程发送信号时,不应出现权限问题。 - Alex P.
太棒了,亚历克斯。完美解决了。祝你周末愉快。 - ALM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接