Android webview cannot render youtube video embedded via iframe

If you want to play videos within your WebView you NEED to load the data with a base URL!

DONT do this:

mContentWebView.loadDataWithBaseURL(null, webViewContentString,
            "text/html", "UTF-8", null);

DO THIS INSTEAD:

    //veryVeryVery important for playing the videos!
    mContentWebView.loadDataWithBaseURL(theBaseUrl, webViewConentString,
            "text/html", "UTF-8", null);

The Base URL will be the something like the “original” url of what you are displaying in your WebView. So let’s say you are making a news reader, the WebView's base url will be the url of the original story.

Good Luck!

Also remember to set up your WebView…Like so…

    mContentWebView.setWebChromeClient(new WebChromeClient());
    mContentWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    mContentWebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
    mContentWebView.setWebViewClient(new WebViewClient());
    mContentWebView.getSettings().setJavaScriptEnabled(true);

You need to have hardware acceleration turned on in the Manifest (only available on SDK 14 and above).

Ex. Hardware Acceleration On:

<application
    android:name="com.example.app"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true">
<!-- hardwareAccelerated requires SDK 14 -->
...
</application>

Leave a Comment