13

Can anyone shed any light on why I'm unable to click and play a youtube clip on an Android handset when placed inside a webview?

I've put a very simple app together (it's being extended, but no right away) and it has a web view assigned using the Android SDK.

The rest of the webview works as expected (text, images, css etc), however, the YouTube clips will not show, they just show as black or white rectangles (where the video embed should be).

We use the same view to display content on the iPhone app equivalent, without any issues.

This is using a basic flash embed, the Safari browser on iPhone converts these to H.264 and plays them nicely in QuickTime.

Can Android do the same??

Here is the embed code, as an example....

<object width="120" height="73">
    <param name="movie" value="http://www.youtube.com/watch?v=ZVYIBIlTIQs&feature=youtube_gdata"></param>
    <param name="allowFullScreen" value="true"></param>
    <param name="allowscriptaccess" value="always"></param>
    <embed src="http://www.youtube.com/watch?v=ZVYIBIlTIQs&feature=youtube_gdata" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="120" height="73"></embed>
</object>
Tamás
  • 47,239
  • 12
  • 105
  • 124
Simon Hume
  • 982
  • 3
  • 11
  • 27

3 Answers3

14

I am not sure if this is what you need. Anyway I hope the following be useful. You can use the iframe method that youtube provides to play its videos. If the browser supports html5 will show the video with it, otherwise with flash.

You can use the following code as an example <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/bIPcobKMB94" frameborder="0">

in the above example the video id is bIPcobKMB94. You can change this id and show your video.

You can access a live example of it here

More infromation for youtube iframe

YouTube HTML5 Video Player

Sotiris
  • 38,986
  • 11
  • 53
  • 85
  • This seems to do the trick! Thanks for taking the time to post it, a great help – Simon Hume Aug 20 '10 at 12:13
  • How comes I can't make it work using iframe. Here is my code: ``. Can anybody give me a hint what's wrong? Actually, I didn't see any difference between using iframe or object tag. I'm using Samsung Galaxy S FROYO.VUVKD1 (2.6.32.9) – newman Dec 06 '11 at 05:14
  • @miliu do you have the latest version of Flash? Because android supports Flash you don't view it in HTML5 version but in Flash, so if you have an old Flash version, may an error occur. – Sotiris Dec 06 '11 at 16:22
  • 3
    I just installed the latest flash (v11) on my phone and I still get the same problem: I can see the initial frame with the play triangle. When I click the triangle, it starts spinning and after a second or two, the area turns into black. No error or whatever. By the way, all other video players including YouTube are working just fine. – newman Dec 08 '11 at 15:51
  • Ok, I finally made it work. As I figured out the only thing I was missing was `_webView.getSettings().setPluginState(PluginState.ON_DEMAND)`. I still works even after I uninstalled Adobe Flash Player (v11). – newman Dec 08 '11 at 23:35
  • Using PluginState.ON allows for the video to appear without the user tapping it first. The Kindle Fire seems to be having issues playing these embedded videos, it works but sometimes it bugs out and keeps playing a video without any UI (I can still hear it). – James Wald Jan 12 '12 at 09:42
  • 1
    @miliu can you post your code. I'm still having the problem you mentioned e.g. see only initial frame. – Efi MK Apr 26 '12 at 18:52
2

Unless the user has Froyo this is going to be impossible. However as long as the user has the YouTube app installed (which is standard for most phones I believe) then you can include a link to the YouTube video, and Android will recognize it as such and open it using the YouTube app. It's not as elegant as embedding, but it's much more reliable.

fredley
  • 32,953
  • 42
  • 145
  • 236
0

You would need to have the flash player, which is only available since Android 2.2 (Froyo).

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
  • 1
    OK, are you aware of any other way of embedding YouTube clips with Android? Using HTML5 will require the user to sign up to the beta program, which isn't ideal – Simon Hume Aug 11 '10 at 13:55
  • so you want to display multiple videos in the webview, not only one,right? otherwise i would have said to open the video in youtube app via intent, since youtube is by default on almost any device. – Mathias Conradt Aug 11 '10 at 14:05
  • I use the YouTube API to pull back a dynamic list of clips from a playlist. I did find a post on here mentioning turning on the plugins using webview.setPluginsEnabled(true); – Simon Hume Aug 11 '10 at 14:25
  • yes, this you would need to have enabled anyway, but without a plugin, even this flag doesn't help much. – Mathias Conradt Aug 11 '10 at 14:43
  • think i'm going to have to write it in java instead! http://www.javacodegeeks.com/2010/05/getting-started-with-youtube-java-api.html – Simon Hume Aug 11 '10 at 14:45