Issue
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>
Solution
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>
Answered By - EricHo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.