Thursday 5 November 2020

Can't scrub video in Webview provided by WebViewClient's shouldInterceptRequest

I am overriding WebViewClient's shouldInterceptRequest method to provide a local video stream. The video plays and pauses fine, but the user can't scrub at all--playback just continues from the same spot. How can I make it so the user can change the playback location?

scrubbing not working in webview

Code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val webView = findViewById<WebView>(R.id.webView)
    WebView.setWebContentsDebuggingEnabled(true)
    webView.webViewClient = object : WebViewClient() {
        override fun shouldInterceptRequest( view: WebView?, request: WebResourceRequest?
        ): WebResourceResponse? {
            if (request?.url.toString().contains("1.mp4")) {
                return WebResourceResponse(
                    "video/mp4",
                    "UTF-8",
                    resources.openRawResource(R.raw.movie)
                )
            }
            return super.shouldInterceptRequest(view, request)
        }
    }

    webView.loadDataWithBaseURL(
        "http://fake.com",
        """
        <!DOCTYPE html>
        <html>
            <p>Video 1:</p>
            <p><video src="http://fake.com/1.mp4" /></p>
        </html>
    """.trimIndent(), "text/html", StandardCharsets.UTF_8.name(), null
    )
}

Here is a repo with the problem. Please note that this isn't the real use case, just a minimal example.



from Can't scrub video in Webview provided by WebViewClient's shouldInterceptRequest

No comments:

Post a Comment