1788

Youtube's onStateChange is not fired if play is triggered by postMessage

Question:

If the youtube playVideo is triggered by postMessage, the onStateChange will not be fired. (<a href="http://jsfiddle.net/nedvedyy/smx92nq0/" rel="nofollow">http://jsfiddle.net/nedvedyy/smx92nq0/</a>). Any one runs into it before?

<script src="jquery-1.10.2.min.js"></script> <!-- 1. The <iframe> (and video player) will replace this <div> tag. --> <div id="player"></div> <button class="button">BUTTON</button> <script> // 2. This code loads the IFrame Player API code asynchronously. var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // 3. This function creates an <iframe> (and YouTube player) // after the API code downloads. var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: '0Bmhjf0rKe8', playerVars: { 'controls': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } var playerReady = false; // 4. The API will call this function when the video player is ready. function onPlayerReady(event) { playerReady = true; } // 5. The API calls this function when the player's state changes. // The function indicates that when playing a video (state=1), // the player should play for six seconds and then stop. function onPlayerStateChange(event) { alert(event); } $(".button").on("click", function() { document.getElementById('player').contentWindow.postMessage(JSON.stringify({ 'event': 'command', 'func': 'playVideo', 'args': [] }), "*"); }); </script>

Strangely, if the playVideo is called like below, the onStageChange can be captured.

onPlayerReady=function(event) { console.log("hey Im ready"); event.target.playVideo(); }

Answer1:

Did you complete your homework here? Go through complete manual, your solution is there from where you pasted.

<a href="https://developers.google.com/youtube/iframe_api_reference" rel="nofollow">https://developers.google.com/youtube/iframe_api_reference</a>

Recommend

  • Start another EXE in Managed Code
  • Javascript Array, Object, Date not defined
  • You tube videos are not playing
  • Get the pasted content on document on paste event
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • HTML5 video only works in IE. The other browsers shows the black screen
  • In loopback documentation what does variable 'cb' stands for?
  • Loading .coffee files via a view in Rails
  • Detect when Facebook like button is clicked
  • How to attach a node.js readable stream to a Sendgrid email?
  • Read a local file using javascript
  • FileReader+canvas image loading problem
  • Get one-time binding to work for ng-if
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Volley JsonObjectRequest send headers in GET Request
  • Controls, properties, events and timers running in design time
  • MySQL WHERE-condition in procedure ignored
  • Importing jscolor library in angular 2
  • Hazelcast - OperationTimeoutException
  • Adding custom controls to a full screen movie
  • jquery mobile loadPage not working
  • RestKit - RKRequestDelegate does not exist
  • How to delete a row from a dynamic generate table using jquery?
  • Revoking OAuth Access Token Results in 404 Not Found
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Acquiring multiple attributes from .xml file in c#
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • EntityFramework adding new object to nested object collection
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • java string with new operator and a literal
  • playing video using jmf