Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview cannot render youtube video embedded via iframe

This is about loading youtube videos using latest embedded format (iframe) inside a webview.

Example of the iframe embed format

<iframe width="637" height="358" src="http://www.youtube.com/embed/olC42gO-Ln4?fs=1&amp;feature=oembed" frameborder="0" allowfullscreen=""></iframe>

Test the code on Android 2.3.3 & 3.2 devices (HTC Desire & Asus Transformer), the webview would only show a black rectangle.

I tried a similar embed from vimeo

<iframe src="http://player.vimeo.com/video/35693267" width="640" height="360" frameborder="0"></iframe>

In 2.3, video played correctly
In 3.2, a black rectangle flashed and disappeared, the iframe area is blank.

Finally if the old embed format (using the object tag) is used, the video is displayed properly inside the webview in both 2.3.3 & 3.2.

I have checked related questions and added

android:hardwareAccelerated="true"

in the application and/or activity tag but still no video in both 2.3 & 3.2 devices.

This is a big problem because more websites are now using the newest format (iframe) to embed their youtube videos. Android/Youtube Team, please take a look at this problem.

like image 237
littlestove Avatar asked Jan 27 '12 16:01

littlestove


People also ask

Can YouTube videos can be embedded using an IFrame?

The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript.

How do I embed a YouTube video into an IFrame?

On a computer, go to the YouTube video or playlist you want to embed. From the list of Share options, click Embed. From the box that appears, copy the HTML code. Paste the code into your website HTML.

Why is my YouTube video not embedding?

Allow embedding of your video. If you receive the error message, "Embedding disabled on request” ,you have probably accidentally disabled embedding via YouTube. To grant permission again, follow these steps: Go to “Video Manager.” Select the appropriate video and click “Edit”.

How can I make WebView keep a video or audio playing in the background?

After searching a lot, I found this thread. I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened.


4 Answers

Android browsers are utterly buggy what comes to video playback and embedding. It simply does not work across devices. Trying to get it working is just waste of your time. My suggestion is that you don't try to include <iframe> but simply provide a thumbnail of the video which directly links to YouTube page or h264 file.

Earlier discussion, with a possible solution.

Google Reader-esque optimizing of WebViews on Android

like image 75
Mikko Ohtamaa Avatar answered Oct 12 '22 22:10

Mikko Ohtamaa


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>
like image 37
Sakiboy Avatar answered Oct 12 '22 22:10

Sakiboy


HTML5 Video support

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient.

http://developer.android.com/reference/android/webkit/WebView.html

(Hope it help someone)

like image 22
parser_failed Avatar answered Oct 12 '22 23:10

parser_failed


This worked for me- the code opens youtube site and can play its videos inside WebView:

mWebView = (WebView) findViewById(R.id.webview);     

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);


String frameVideo = "<html><body>Youtube video .. <br> <iframe width=\"320\" height=\"315\" src=\"https://www.youtube.com/\" frameborder=\"0\" allowfullscreen></iframe></body></html>";

mWebView.loadData(frameVideo, "text/html", "utf-8");

mWebView.loadUrl("http://www.youtube.com/");

mWebView.setWebViewClient(new WebViewClient());
like image 42
user2924714 Avatar answered Oct 12 '22 22:10

user2924714