Android 从 Google 驱动器流式传输视频
Android Stream video from Google drive
我正在构建一个 Android 应用程序,它需要从 Google 流式传输视频 link 就像这样:https:// docs.google.com/file/d/--ID--
我无法获取 rtsp,所以它无法 运行 videoview 中的视频,并且它不以 mp4 或 3gp 结尾...所以我不能 运行 就是这样:
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 中的视频:
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.loadUrl("https:// docs.google.com/file/d/--ID--");
webview.setWebChromeClient(new WebChromeClient());
但是视频不能全屏播放,不能暂停,而且很卡...
那我该怎么办?无论如何可以从 Google 驱动器
流式传输视频
因为我也在尝试这个,我可以自己找到解决方案
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;
}
现在我使用这三个函数后,我可以通过 readtIt(InputStream stream)
得到一个直接视频 url return 作为一个字符串,我可以用它来解析 VideoView.
我正在构建一个 Android 应用程序,它需要从 Google 流式传输视频 link 就像这样:https:// docs.google.com/file/d/--ID--
我无法获取 rtsp,所以它无法 运行 videoview 中的视频,并且它不以 mp4 或 3gp 结尾...所以我不能 运行 就是这样:
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 中的视频:
webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setPluginState(WebSettings.PluginState.ON); webview.loadUrl("https:// docs.google.com/file/d/--ID--");
webview.setWebChromeClient(new WebChromeClient());
但是视频不能全屏播放,不能暂停,而且很卡...
那我该怎么办?无论如何可以从 Google 驱动器
流式传输视频因为我也在尝试这个,我可以自己找到解决方案
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;
}
现在我使用这三个函数后,我可以通过 readtIt(InputStream stream)
得到一个直接视频 url return 作为一个字符串,我可以用它来解析 VideoView.