Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

webview - Android Stream video from Google drive

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--

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 intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("https:// docs.google.com/file/d/--ID--"), "video/mp4"); view.getContext().startActivity(intent);

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.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 + "
");
                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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...