来自 Google Drive 的 Android Stream 视频 [英] Android Stream video from Google drive
问题描述
我正在构建一个需要从 Google 驱动器流式传输视频的 Android 应用,视频链接是这样的:https://docs.google.com/file/d/--ID--
i am building an Android app that need to Stream video from Google drive the video link is like that : https:// docs.google.com/file/d/--ID--
我无法获得 rtsp,因此它无法在 videoview 中运行视频,并且它不会以 mp4 或 3gp 结尾......所以我无法像那样运行它:
i can't get the rtsp so it can't run the video in a videoview and it doesn't end with mp4 or 3gp... so i can't run it like that :
意图意图=新意图(Intent.ACTION_VIEW);intent.setDataAndType(Uri.parse("https://docs.google.com/file/d/--ID--"),视频/mp4");view.getContext().startActivity(intent);
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("https:// docs.google.com/file/d/--ID--"), "video/mp4"); view.getContext().startActivity(intent);
我能够使用以下代码在 webView 中运行视频:
i was able to run the video in webView using this code :
webview.getSettings().setJavaScriptEnabled(true);webview.getSettings().setPluginState(WebSettings.PluginState.ON);webview.loadUrl("https://docs.google.com/file/d/--ID--");
webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setPluginState(WebSettings.PluginState.ON); webview.loadUrl("https:// docs.google.com/file/d/--ID--");
webview.setWebChromeClient(new WebChromeClient());
webview.setWebChromeClient(new WebChromeClient());
但是视频不能全屏播放,不能暂停,而且滞后...
but the video can't be played full screen and it can't be paused and it lag ...
那我该怎么办?无论如何可以从谷歌驱动器流式传输视频
so what should i do ? is there anyway to stream the video from Google drive
推荐答案
因为我也在尝试这个,我可以自己找到解决方案
As I am trying this also and I can find a solution by myself
1:确保视频网址是 https://drive.google.com/file/d/VIDEO-ID/预览"
1: make sure video url is https://drive.google.com/file/d/VIDEO-ID/preview"
2:我从上面的网址下载网页内容并获得直接的视频网址:
2: I download web content from the above url and get direct video url:
public String downloadUrl(String myurl) throws IOException {
InputStream is = null;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
String contentAsString = readIt(is);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
//从流输出中获取直接视频url
//Get direct video url from stream output
public String readIt(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("fmt_stream_map")) {
sb.append(line + "
");
break;
}
}
reader.close();
String result = decode(sb.toString());
String[] url = result.split("\|");
return url[1];
}
//我们需要一个函数来解码url才能正常使用
//We need a function to decode url to normal use
public String decode(String in) {
String working = in;
int index;
index = working.indexOf("\u");
while (index > -1) {
int length = working.length();
if (index > (length - 6)) break;
int numStart = index + 2;
int numFinish = numStart + 4;
String substring = working.substring(numStart, numFinish);
int number = Integer.parseInt(substring, 16);
String stringStart = working.substring(0, index);
String stringEnd = working.substring(numFinish);
working = stringStart + ((char) number) + stringEnd;
index = working.indexOf("\u");
}
return working;
}
在我使用这三个函数之后,我可以得到一个由 readtIt(InputStream stream)
作为字符串返回的直接视频 url,我可以用它来解析 VideoView.
After i use thes three function now I can get a direct video url that return by readtIt(InputStream stream)
as a string and I can use it for parsing to VideoView.
这篇关于来自 Google Drive 的 Android Stream 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!