如何从 Android 中的视频 URL 捕获/录制剪辑并保存到手机 [英] How to capture/record clip from Video URL in Android and save to phone

查看:101
本文介绍了如何从 Android 中的视频 URL 捕获/录制剪辑并保存到手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中,是否可以从视频 URL(例如:http://www.test.com/video.mp4)?

In Android, is it possible to record a short clip (ex: an arbitrary 5-10 seconds in the video) from a Video URL (ex: http://www.test.com/video.mp4)?

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

For example, I'd like to stream a video (from url) in an Activity and allow the ability to capture/record a short clip from it. Perhaps, allow the user to record an arbitrary Start/End time from the video. If so, is there an API to accomplish this? If not, is there an Android library to support this?

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

Please provide a sample code solution for this.

推荐答案

可以查看 这个 链接.总之你的服务器必须支持下载.如果是,您可以尝试以下代码:

You can see this link. In short your server has to support downloading. If it does, you can try the following code:

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
    }
  }
}

如果服务器不支持下载,您无能为力.

If the server doesn't support downloading, there is nothing you can do.

这篇关于如何从 Android 中的视频 URL 捕获/录制剪辑并保存到手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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