Android Stream Video from Google Drive

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



Leave a reply



Submit