Hidden youtube player loses its methods

Posted by zaius on Stack Overflow See other posts from Stack Overflow or by zaius
Published on 2010-02-04T06:48:17Z Indexed on 2010/03/28 19:03 UTC
Read the original article Hit count: 280

Filed under:
|
|

I'm controlling a embedded youtube chromeless player with javascript, and I want to hide it occasionally by setting display: none. However, when I show the player again, it loses its youtube methods.

For example:

<script>
  swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player",
    "player", "425", "356", "8", null, null, 
    {allowScriptAccess: "always"}, {id: 'player'}
  );

  var player = null;

  function onYouTubePlayerReady(playerId) {
    player = document.getElementById(playerId);
    player.addEventListener('onStateChange', 'playerStateChanged');
  }

  function hidePlayer() {
    player.pauseVideo();
    player.style.display = 'none';
  }
  function showPlayer() {
    player.style.display = 'block';
    player.playVideo();
  }
</script>
<a href="#" onClick="hidePlayer();">hide</a>
<a href="#" onClick="showPlayer();">show</a>
<div id="player"></div>

Calling hidePlayer followed by showPlayer gives this error on the playVideo call:

Uncaught TypeError: Object #<an HTMLObjectElement> has no method 'playVideo'

The only solution I can find is to use visibility: hidden, but that is messing with my page layout. Any other solutions out there?

© Stack Overflow or respective owner

Related posts about flash

Related posts about youtube