Reputation: 2284
I want to have a HTML5 app with an embedded video player.
Normally I would simply use the <video>
tag where appropriate. But I need to tell android to use a different player than the default one used for this tag. Is there a way to archive that?
Can I for example make it use an self compiled ExoPlayer? Or am I stuck with whatever is hidden behind the <video>
tag?
Upvotes: 2
Views: 359
Reputation: 5087
Your webview can communicate with your Java/Kotlin code via addJavascriptInterface.
Start by creating a class that you will later set as the WebView
's JavaScript interface. Annotate the method(s) you want to expose to JavaScript with @JavascriptInterface
. You can also add arguments to these method(s):
class MyJsInterface() {
@JavascriptInterface
fun onShowNativePlayer() {
// Add your logic to show the native player instead of the HTML5.
}
}
Create an instance of the above class and pass it to your WebView
:
myWebView.addJavascriptInterface(MyJsInterface(), "android_js_interface")
And, finally, this is how you can trigger the exposed Java/Kotlin method(s) from your JavaScript code:
// This value should match the second argument that you passed in addJavascriptInterface.
var ANDROID_JS_INTERFACE = "android_js_interface"
...
// Call your Java/Kotlin method.
window[ANDROID_JS_INTERFACE].onShowNativePlayer()
Bonus:
You can also achieve the reverse (ie. call JavaScript methods from your Java/Kotlin code):
myWebView.evaluateJavascript("myJsFunc()", null)
Where myJsFunc()
is a function you defined in your JavaScript code:
// You can also pass arguments.
window.myJsFunc = function() {
// Do something here.
}
Upvotes: 1
Reputation: 2284
I guess one way would be to bundle my own compiled browser that has the video player replaced. But this would be over the top here.
If nothing else turns up, I will opt for using a video fragment in the background with a transparent HTML5 App in front as a compromise.
Upvotes: 0