gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:24 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com