如何从Android中的视频URL捕获/录制剪辑并保存到手机

cod*_*ark 9 android android-video-player android-file android-videoview

在Android中,是否可以从视频URL(例如:http://www.test.com/video.mp4)录制短片段(例如视频中任意5-10秒)?

例如,我想在一个Activity中传输一个视频(来自url),并允许从中捕获/记录一个短片段.也许,允许用户从视频中录制任意开始/结束时间.如果是这样,是否有API来完成此任务?如果没有,是否有支持此功能的Android库?

请为此提供示例代码解决方案.

Kir*_*rov 4

你可以看到这个链接。简而言之,你的服务器必须支持下载。如果是这样,您可以尝试以下代码:

private final int TIMEOUT_CONNECTION = 5000; //5sec
private final int TIMEOUT_SOCKET = 30000; //30sec
private final int BUFFER_SIZE = 1024 * 5; // 5MB

private final int TIMEOUT_CONNECTION = 5000; //5sec
private final int TIMEOUT_SOCKET = 30000; //30sec
private final int BUFFER_SIZE = 1024 * 5; // 5MB

try {
  URL url = new URL("http://....");

  //Open a connection to that URL.
  URLConnection ucon = url.openConnection();
  ucon.setReadTimeout(TIMEOUT_CONNECTION);
  ucon.setConnectTimeout(TIMEOUT_SOCKET);

  // Define InputStreams to read from the URLConnection.
  // uses 5KB download buffer
  InputStream is = ucon.getInputStream();
  BufferedInputStream in = new BufferedInputStream(is, BUFFER_SIZE);
  FileOutputStream out = new FileOutputStream(file);
  byte[] buff = new byte[BUFFER_SIZE];

  int len = 0;
  while ((len = in.read(buff)) != -1)
  {
      out.write(buff,0,len);
  }
} catch (IOException ioe) {
  // Handle the error
} finally {
  if(in != null) {
    try {
      in.close();
    } catch (Exception e) {
      // Nothing you can do
    }
  }
  if(out != null) {
    try {
      out.flush();
      out.close();
    } catch (Exception e) {
      // Nothing you can do
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果服务器不支持下载,则无能为力。