Search code examples
androidkotlinvideowebview

Webview not plays video


I have a function:

private fun WebView.initVideo(video: String) {
    WebViewVideoPlayerUtils.addCookie(video, this)
    with(settings) {
        javaScriptEnabled = true
        domStorageEnabled = true
        cacheMode = WebSettings.LOAD_NO_CACHE
        builtInZoomControls = true
        allowFileAccess = true
        loadWithOverviewMode = true
        useWideViewPort = true
    }
    loadDataWithBaseURL(
        null,
        video,
        "text/html",
        "utf-8",
        null
    )
}

And it worked fine. But after updating project it's not playing video. Just shows String

<iframe width=“853” height=“480” src=“https://www.youtube.com/embed/_iATsZKqYF0" frameborder=“0” allow=“accelerometer;autoplay;encrypted-media;gyroscope;picture-in-picture” allowfullscreen></iframe>

enter image description here

How could I Fix it?

P.S. AndroidManifest.xml contains needed permission and

android:hardwareAccelerated="true" android:usesCleartextTraffic="true"

P.P.S: Using logs I find out that parameter video comes as a String:

&lt;iframe width=&quot;853&quot; height=&quot;480&quot; src=&quot;https://www.youtube.com/embed/_iATsZKqYF0&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;

And it works fine if I manually change it to

"<iframe width=\"853\" height=\"480\" src=\"" + "https://www.youtube.com/embed/_iATsZKqYF0" + "\" frameborder=\"0\" allow=\"accelerometer;autoplay;encrypted-media;gyroscope;picture-in-picture\" allowfullscreen/>"

As if Android can't parse it I have a lot of screens using this strings for playing video. So how could I resolve this problem in one place?


Solution

  • It was just the encoding mistake on the server side. Adding the line

    val parsedVideo = Html.fromHtml(video, Html.FROM_HTML_MODE_LEGACY).toString()
    

    resolved the issue.