1

html

<html>
<head>
<style type="text/css">    body {    background-color: transparent;    color: white;    }    
</style>    
</head>
<body style="margin:0">    
<embed id="yt" src="http://www.youtube.com/v/6eK-W32IME0?fs=1&hl=en_US&enablejsapi=1" type="application/x-shockwave-flash" width="330" height="200"></embed>
</body>
</html>

The code I'm running to try to play the video is

var yt = document.getElementById("yt");
yt.playVideo();

The JS error I get is

TypeError: Object #<HTMLEmbedElement> has no method 'playVideo'

But it seems like this is how everyone else does it. What's the deal?

** EDIT **

jsfiddle

** EDIT **

The current JSFiddle now works fine on a pc/mac, but not on an iPad.

Community
  • 1
  • 1
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

2 Answers2

4

I think you need to add &enablejsapi=1 to the querystring of the URL referencing the video in your <embed> object.

Also, try replacing your <embed> element with an <object> element. Here's a working example.

A note about iOS/Mobile Webkit:

Apparently, according to this answer, Apple explicitly prohibits automated control of embedded media in Safari or other Mobile Webkit browsers, requiring the iOS user to interact directly with the video object to initiate playback. This goes for object and embed elements, as well as HTML5 video and audio elements.

Community
  • 1
  • 1
Aaron
  • 5,137
  • 1
  • 18
  • 20
  • Works great on a computer, but not on the iPad for some reason. It has to work there so I'm perplexed atm. – Jacksonkr Apr 02 '12 at 17:07
  • Well, that's because iOS devices don't support Flash. Period. If you use an iframe embed the video on your page, YouTube will intelligently detect the viewers' devices and serve the appropriate version of the video for each one. Then, however, you'll lose the script control you're trying to implement. Long story short: you'll need to detect Flash capability on your page and generate an `object` or `video` tag based on the result of that test. – Aaron Apr 02 '12 at 20:26
  • 1
    I should point out that the video playing actually DOES work on the ipad (youtube is doing something fancy here). It's just that the `playVideo` function is kind of busted in the ipad. – Jacksonkr Apr 02 '12 at 21:36
  • 2
    Apparently, according to this answer (http://stackoverflow.com/a/9028877/1030243), Apple explicitly prohibits automated control of embedded media in Safari, requiring the iOS user to interact directly with the video object to initiate playback. – Aaron Apr 02 '12 at 23:42
  • Aaron, maybe you should move your last Comment to the Answer. It's an important piece of info. Damn Apple, not even in Chrome we can initiate the playback externally. – brasofilo Oct 30 '13 at 16:29
  • Good thinking. Edited. – Aaron Oct 30 '13 at 19:33
1

you're not loading the JS api try this:

<embed id="yt" src="http://www.youtube.com/v/6eK-W32IME0?fs=1&hl=en_US&enablejsapi=1" type="application/x-shockwave-flash" width="330" height="200"></embed>   

to learn more about youtubes JS api check out https://developers.google.com/youtube/js_api_reference

JKirchartz
  • 17,612
  • 7
  • 60
  • 88