作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Android 中,是否可以从视频 URL(例如:http://www.test.com/video.mp4)录制短片(例如:视频中的任意 5-10 秒)?
例如,我想在 Activity 中流式传输视频(来自 url),并允许从中捕获/录制短片。也许,允许用户从视频中记录任意开始/结束时间。如果是这样,是否有 API 可以实现此目的?如果没有,是否有支持此功能的 Android 库?
请为此提供示例代码解决方案。
最佳答案
可以看到this链接。简而言之,您的服务器必须支持下载。如果是,您可以尝试以下代码:
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
}
}
}
如果服务器不支持下载,你也无能为力。
关于android - 如何在 Android 中从视频 URL 捕获/录制剪辑并保存到手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366835/
我是一名优秀的程序员,十分优秀!