BhieNEka

Youtube Auto Play & Mute

Oct 25th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.27 KB | None | 0 0
  1. <script src="http://www.youtube.com/player_api"></script>
  2. <div id="player"></div>
  3. <script>
  4. var player;
  5.  
  6. function onYouTubePlayerAPIReady() {
  7.     player = new YT.Player('player', {
  8.        
  9.         playerVars: {
  10.             'autoplay': 1,
  11.             'controls': 0,
  12.             'autohide': 1,
  13.             'wmode': 'opaque',
  14.             'showinfo': 1,
  15.             'mute': 1,
  16.             'loop': 1,
  17.             //'start': 15,
  18.             //'end': 110,
  19.             'list': 'PLZ4N99MMo_3KNAMAPvfxYW68WQJ1jBZ9k',
  20.         },
  21.         height: '0',
  22.         width: '0',
  23.         videoId: '7e4VZfS5ElY',
  24.         events: {
  25.             'onReady': onPlayerReady
  26.         }
  27.     });
  28.  
  29. }
  30.  
  31. function onPlayerReady(event) {
  32.     event.target.mute();
  33.     $('#text').fadeIn(400);
  34.     //why this? Well, if you want to overlay text on top of your video, you
  35.     //will have to fade it in once your video has loaded in order for this
  36.     //to work in Safari, or your will get an origin error.
  37. }
  38.  
  39. //this pauses the video when it's out of view, just wrap your video in .m-//video
  40. $(window).scroll(function() {
  41.    var hT = $('.m-video').height(),
  42.        wS = $(this).scrollTop();
  43.    if (wS > hT) {
  44.       player.pauseVideo();
  45.    }
  46.    else {
  47.       player.playVideo();
  48.    }
  49. });
  50. </script>
Advertisement
Add Comment
Please, Sign In to add comment