Pause Youtube embed when playing Soundcloud embed

前端 未结 2 1271
孤街浪徒
孤街浪徒 2020-12-31 23:04

I have a music blog that contains various embedded soundcloud and youtube players.

What I want to do is prevent any audio from playing simultaneously. In other words

相关标签:
2条回答
  • 2020-12-31 23:34

    it's possible. check widget api playground. you can watch all state events from soundcloud iframe widget. this can be done on flash embed too. https://w.soundcloud.com/player/api_playground.html

    watch for SC.Widget.Events.PLAY {"loadedProgress":0,"currentPosition":0,"relativePosition":0} event on start playing

    0 讨论(0)
  • 2020-12-31 23:46

    First of all, things will be greatly simplified if you are able to get an ID onto the iframes themselves, like this:

    <span class="soundcloud_embed">
      <iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F4997623" id="sound1"></iframe>
    </span>
    <span class="soundcloud_embed">
      <iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F105153133" id="sound2"></iframe>
    </span>
    <span class="youtube_embed">
      <iframe width="400" height="225" src="//www.youtube.com/embed/tv8WgLEIPBg" id="vid1" frameborder="0" allowfullscreen></iframe>
    </span>
    <span class="youtube_embed">
      <iframe width="400" height="225" src="//www.youtube.com/embed/FmICU1gMAAw" id="vid2" frameborder="0" allowfullscreen></iframe>
    </span>
    

    If you'd prefer not to hack the auto_html gem that you mention in order to get IDs, you can either use jquery's .uniqueId() function to generate them on all iframes or use the getFrameID() helper method from the post you referred to (and seem to already be using).

    To make it as easy as possible to get the two APIs (Youtube and Soundcloud) to be able to respond to each other's events, you can use a couple of control objects that keep track of which players are on your page and which of them is currently playing (this strategy is similar to that employed by the links you referred to, but are expanded to be able to keep track of which player belongs to which API). With it, define a generic pause function that serves as a simplistic wrapper for both APIs:

    var playerCurrentlyPlaying = {"api":null,"frameID":null};
    var players = {"yt":{},"sc":{}};
    
    pauseCurrentPlayer=function() {
      var api=playerCurrentlyPlaying["api"],
          frameid=playerCurrentlyPlaying["frameID"];
    
      switch(api) {
        case "yt":
          players[api][frameid].pauseVideo();
          break;
        case "sc":
          players[api][frameid]["widget"].pause();
          break;
      }
    };
    

    Next, you'll want to define two separate functions; the first will be called whenever a YouTube play event is captured, and the second whenever a Soundcloud play event is captured. Currently the Soundcloud HTML5 Widget has a few bugs that force the necessity to include some pretty ugly hacks--namely, the Play event sometimes isn't fired when you play a Soundcloud sound for the first time. Luckily, the Play_Progress event is, so we can leverage that, but must also include a workaround so it doesn't create a race condition when pausing a sound and starting a video:

    onYTPlay =function(frameid) {
      if (playerCurrentlyPlaying["frameID"]!=frameid && playerCurrentlyPlaying["frameID"]!=null) {
         pauseCurrentPlayer();
      }
      playerCurrentlyPlaying["api"]="yt";
      playerCurrentlyPlaying["frameID"]=frameid;
    };
    
    onSCPlay=function(frameid,event) {
      if (event==SC.Widget.Events.PLAY||players["sc"][frameid]["firstplay"]==true) {
         if (playerCurrentlyPlaying["api"]=="yt") { // because any soundcloud player will be automatically paused by another soundcloud event, we only have to worry if the currently active player is Youtube
            pauseCurrentPlayer();
         }
         playerCurrentlyPlaying["api"]="sc";
         playerCurrentlyPlaying["frameID"]=frameid;
         players["sc"][frameid]["firstplay"]=false;
      }
    };
    

    Finally, you can then add your hooks to the Youtube embeds and the Soundcloud embeds, remembering the hack we have to put in for the Soundcloud Play event. Don't forget to embed the Soundcloud Widget API (<script src="w.soundcloud.com/player/api.js"; type="text/javascript"></script>), and then use this code to do your bindings:

    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    function onYouTubeIframeAPIReady() {
      $(".youtube_embed iframe").each(function() {
        players["yt"][$(this).attr('id')] = new YT.Player($(this).attr('id'), {
          events: { 'onStateChange': onYTPlayerStateChange }
        });
      });
    }
    
    onYTPlayerStateChange = function(event) {
      if (event.data==YT.PlayerState.PLAYING) {
         onYTPlay(event.target.a.id);
      }
    };
    
    (function(){
        $(".soundcloud_embed iframe").each(function() {
          var frameid=$(this).attr('id');
          players["sc"][frameid]={};
          players["sc"][frameid]={"widget":SC.Widget(document.getElementById(frameid)),"firstplay":true};
          players["sc"][frameid]["widget"].bind(SC.Widget.Events.READY, function() {
           players["sc"][frameid]["widget"].bind(SC.Widget.Events.PLAY, function() {
            onSCPlay(frameid,SC.Widget.Events.PLAY);
           });
           players["sc"][frameid]["widget"].bind(SC.Widget.Events.PLAY_PROGRESS, function() {
            onSCPlay(frameid,SC.Widget.Events.PLAY_PROGRESS);
           });
         });
        });
       }());
    

    This approach is set up to handle multiple players of both APIs, and should be decently extensible if you want to support other embeddable media widgets (provided they expose an event when they start to play and they have the ability to programmatically pause players).

    0 讨论(0)
提交回复
热议问题