Android Stream video from Google drive
As I am trying this also and I can find a solution by myself
1: make sure video url is https://drive.google.com/file/d/VIDEO-ID/preview"
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();
}
}
}
//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 + "\n");
break;
}
}
reader.close();
String result = decode(sb.toString());
String[] url = result.split("\\|");
return url[1];
}
//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;
}
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.
How to play videos from a Google Drive link?
After search around the web, finally found the answer. So, if we want to play video from Google Drive, we must get direct download link from our video. In example, we got this link from our video in google drive 'https://drive.google.com/uc?export=download&id=xxxxxxxx'.
And we can put direct download link into :
_controller = VideoPlayerController.network('https://drive.google.com/uc?export=download&id=xxxxxxxx');
Then the video can be play.
Related Topics
Android: HTML Anchor Link Works Only Once in Webview
JSONarray Cannot Be Converted to JSONobject Error
Android Studio - Error:Program Type Already Present
A Correct Way to Convert Byte[] in Java to Unsigned Char* in C++, and Vice Versa
Read Logs from All Apps on Android from Within an App for Android 4.2+
Android Layout with Square Buttons
Change Fill Color on Vector Asset in Android Studio
Bypass Android's Hidden API Restrictions
Android - Change Custom Title View at Run Time
How to Pass Image Data from One Activity to Another Activity
Onbackpressed to Hide Not Destroy Activity
Gradle Dsl Method Not Found: 'Implementation()'
Android: Listview.Getscrolly() - Does It Work
Eclipse Indexer Errors When Using Stl with Android Ndk
Android/Java: How to Delay Return in a Method
App Crash After Activity Has Been Killed in Background