我正在构建一个Android应用程序,需要从Google drive流式传输视频。视频链接是这样的:https://docs.Google.com/file/d/--id--
我不能得到rtsp,所以它不能运行视频在videoview和它没有结束与mp4或3GP...所以我不能这样运行:
Intent Intent=新的意图(Intent.action_view);Intent.SetDataAndType(uri.parse(“https://docs.google.com/file/d/--id-”),“video/mp4”);view.getContext().StartActivity(intent);
我能够使用以下代码在webView中运行视频:
WebView.GetSettings().SetJavaScriptEnabled(true);WebView.getSettings().SetPluginState(WebSettings.pluginState.on);webview.loadURL(“https://docs.google.com/file/d/--id--”);
webview.setWebChromeClient(新建WebChromeClient());
但视频不能全屏播放,也不能暂停、滞后……
那我该怎么办呢?有没有从Google drive流式播放视频
因为我也在尝试这一点,我可以自己找到一个解决办法
1:确保视频url为https://drive.google.com/file/d/video-id/preview“
2:我从上述url下载网页内容,获得直接视频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
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 + "\n");
break;
}
}
reader.close();
String result = decode(sb.toString());
String[] url = result.split("\\|");
return url[1];
}
//我们需要一个函数将url解码为正常使用
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;
}
在我使用了这三个函数之后,我现在可以获得一个直接的视频url,该url通过readtit(inputstreamstream)
作为字符串返回,我可以使用它来解析VideoView。