-1

So, I have a web server that has an linked video on it, I have it accessed to my Web view, and I can play the video using HTML5 Player, but the problem is, the video must be played by a video player application that is installed on my phone... How do I achieve this?

I have tried searching, people seems like to use Youtube Video Player or HTML5 Player, not using their video player application, so I got nothing...

Update

So, after I tried to implement method suggested by @EricHo

webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("mp4")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse(url), "video/mp4");
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });

What I got is, the player that playing the video is the one that built in by Chrome, not by the Android Video Player itself Photo 1, besides what I expect is The video will be played by gallery instead of Chrome player

If necessary, here is my index.html source code

<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
    <title>video</title>
    <script type="text/javascript" src="src.js"></script>
    <link rel="stylesheet" type="text/css" href="src.css"> 
</head>
<body bgcolor="#333">
<center>
<video controls preload="metadata" style=" width:px;  height:px;">
    <source src="video-url" type="video/mp4">
</video><br />

</body>
</html>
sterben
  • 103
  • 1
  • 8

2 Answers2

1

One way is to intercept the HTTP request from the Webview. refer to this

Then check if the request URL is a video url. if yes, open the url using Intent

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(videoPath), "video/mp4");
startActivity(intent);

Edit: Below is my working example, when you click on a link that ends with "mp4", android will open the url with default media players.

    webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            println("shouldOverrideUrlLoading")
            println(request?.url.toString())
            val url :String = request?.url.toString()
            if (url.endsWith("mp4")) {
                val intent = Intent(Intent.ACTION_VIEW)
                intent.setDataAndType(Uri.parse(url), "video/mp4")
                startActivity(intent)
                return true
            }
            return false
        }
    }
    val videoPath = "http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5"
    webView.loadUrl(videoPath)

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ecl.webtest">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
EricHo
  • 183
  • 10
  • So do you mean I need to list links like youtube link, vimeo link or etc to compare? – sterben Oct 28 '19 at 08:52
  • Maybe you check if the link ends with ".mp4", then pass the link to `Intent(Intent.ACTION_VIEW)` to open with video players in android – EricHo Oct 28 '19 at 08:56
  • Alright, let me try this first, I will update if it worked or not – sterben Oct 28 '19 at 09:08
  • Alright I got stuck here, how did I display the HTTP request after I declare WebViewClient.shouldInterceptRequest(webview,url) ? – sterben Oct 29 '19 at 03:12
  • https://stackoverflow.com/a/4805033/6854435 I suggest you try this code, the url can be accessed by overriding the `#shouldOverrideUrlLoading()` method – EricHo Oct 29 '19 at 03:39
  • I think it is easier to open the default video player when the user clicks on a link than capturing video in – EricHo Oct 29 '19 at 06:50
-1

i might be wrong but your default android player ,plays a video if it is downloaded in a tmp folder or other folders on your phone. you seem to be able to play the linked video on your page using html video controls but it wont be played with your android player because it wasnt downloaded and identified as a video file. you can create an hyperlink on the page with the link to the video as the url and lets see if the default player will pick it up

  • well, the problem is that the video is could be any internet video so it can't be using static hyperlink – sterben Oct 28 '19 at 09:10