Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube JavaScript API - Pause audio when either youtube video plays

I have background noise on a website which the client wants playing on loop throughout the website, so I have set up an <audio> tag which autoplays and loops.

On several pages, I have multiple Youtube embedded iframes which are declared using the embedded code from the youtube video pages, for example:

<iframe width="420" height="315" src="http://www.youtube.com/embed/Y3OR9HnB14I?rel=0" frameborder="0" allowfullscreen></iframe>

What they want, is that when a youtube video is played / resumed, the audio pauses and when a youtube video is stopped / paused then resume the audio.

Has anyone got an idea on how to do this using the Youtube Javascript API?

like image 576
Stefan Dunn Avatar asked Jun 06 '13 14:06

Stefan Dunn


2 Answers

You should be able to use the Youtube Javascript Player API to do this. You can listen to the onStateChange event on all your embedded players, when a "1" (playing) comes from any of them, you pause the . When either "2" (paused) or "0" (ended) comes you can resume the again.

This works for me:

<html>
  <head>
    <script language="javascript">
      var player = null;
      var lobbyAudio = null;

      function onYouTubePlayerReady(playerapiid) {
        player = document.getElementById(playerapiid);
        lobbyAudio = document.getElementById('lobby-audio');
        player.addEventListener("onStateChange", "onYouTubePlayerStateChange");
      }

      function onYouTubePlayerStateChange(state) {
        switch (state) {
          case 1:  // playing
            lobbyAudio.pause();
            break;
          case 0:  // ended
          case 2:  // paused
            lobbyAudio.play();
            break;
        }
      }
    </script>
  <body>
    <audio id="lobby-audio" src="lobby-audio.mp3" autoplay="True" controls="True" loop="True"></audio>

    <object width="640" height="360">
      <param name="movie"
      value="https://www.youtube.com/v/t-aOvEweUEw?enablejsapi=1&playerapiid=ytplayer1&version=3"></param>
      <param name="allowFullScreen" value="true"></param>
      <param name="allowScriptAccess" value="always"></param>
      <embed
          id="ytplayer1"
          src="https://www.youtube.com/v/t-aOvEweUEw?enablejsapi=1&playerapiid=ytplayer1&version=3"
          type="application/x-shockwave-flash" allowfullscreen="true"
          allowScriptAccess="always" width="640" height="360">
      </embed>
    </object>
  </body>
</html>

I leave it as an exercise for you to figure out how to keep track of the state for several videos =) But a hint is that the onYouTubePlayerReady() is called once for every player on the page. You can specify a different playerapiid for each player to tell them apart. (in the example above i used "ytplayer1" for the single player.

  • API Reference: https://developers.google.com/youtube/js_api_reference
  • API demo page: https://developers.google.com/youtube/youtube_player_demo
  • The refrence to could be useful too: http://www.w3schools.com/tags/ref_av_dom.asp
like image 102
Christian Avatar answered Nov 14 '22 22:11

Christian


from the API docs:

Events

onStateChange This event fires whenever the player's state changes. The value that the API passes to your event listener function will specify an integer that corresponds to the new player state. Possible values are:

    -1 (unstarted)
    0 (ended)
    1 (playing)
    2 (paused)
    3 (buffering)
    5 (video cued).

When the player first loads a video, it will broadcast an unstarted (-1) event. When a video is cued and ready to play, the player will broadcast a video cued (5) event. In your code, you can specify the integer values or you can use one of the following namespaced variables:

    YT.PlayerState.ENDED
    YT.PlayerState.PLAYING
    YT.PlayerState.PAUSED
    YT.PlayerState.BUFFERING
    YT.PlayerState.CUED

And for play/pause the video use:

Playing a video

player.playVideo():Void Plays the currently cued/loaded video. The final player state after this function executes will be playing (1).

Note: A playback only counts toward a video's official view count if it is initiated via a native play button in the player.

player.pauseVideo():Void Pauses the currently playing video. The final player state after this function executes will be paused (2) unless the player is in the ended (0) state when the function is called, in which case the player state will not change.

like image 22
Simone Pessotto Avatar answered Nov 14 '22 23:11

Simone Pessotto