2

Sorry if this looks like a repost of an earlier problem of mine, but it isn't. It's a different problem of a different approach.

I've got this iframe:

<iframe id="video1" width="970" height="546" src="https://youtube.com/embed/9IYRC7g2ICg?enablejsapi=1&autoplay=1&controls=0&loop=1&playlist=9IYRC7g2ICg&showinfo=0&modestbranding=0" frameborder="0" allowfullscreen></iframe>

Now to manually pause the video when clicked on a custom button, this works fine:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $('#button1').click(function() {
            $('#video1')[0].contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
        });
    });
</script>

pauseVideo is a command listed here https://developers.google.com/youtube/iframe_api_reference.

My question is: i'm somehow completely unable to use commands that expect arguments, for example seekTo(seconds:Number, allowSeekAhead:Boolean).

For jumping to 0:20 in the video, I tried:

$('#video1')[0].contentWindow.postMessage('{"event":"command","func":"seekTo","args":"20, true"}', '*');

but with no success. I'm probably putting the arguments in the wrong place, since this is the first time I'm working with the postMessage function for iframes and I kinda feel a bit newbie-like in that sense.

Could anybody help me? :)

rails_has_elegance
  • 1,590
  • 4
  • 21
  • 37
  • 1
    Did you try `"args":[20,true]`? – Florian Gl Sep 05 '14 at 11:42
  • Out of curiosity (and this is sincere curiosity ... not a backhanded attempt to suggest you're doing something wrong), why are you using postMessage instead of creating an object with the YouTube iframe API? What benefits might this method yield? – jlmcdonald Sep 06 '14 at 04:11
  • I originally did it via postMessage because it appeared easier to me than loading the youtube api and initializing the player object with it (on my first attempt it didn't work out). Actually by now, now I'm doing it the normal way youtube api way and it works nice. – rails_has_elegance Sep 10 '14 at 10:29
  • Follow this link it will help for sure. [http://stackoverflow.com/questions/7443578/youtube-iframe-api-how-do-i-control-a-iframe-player-thats-already-in-the-html][1] [1]: http://stackoverflow.com/questions/7443578/youtube-iframe-api-how-do-i-control-a-iframe-player-thats-already-in-the-html – Vishal Tarkar Mar 05 '15 at 07:42
  • See this answer for full explanation of how to do this and other things with YouTube iframes: http://stackoverflow.com/a/7513356/1060487 – mattdlockyer May 04 '16 at 16:40

1 Answers1

4

Try this:

var data = {event: 'command', func: 'seekTo', args: [20, true]};
var message = JSON.stringify(data);

$('#video1')[0].contentWindow.postMessage(message, '*');