Last active
August 29, 2015 13:56
-
-
Save b4blue/8818710 to your computer and use it in GitHub Desktop.
Youtube with javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="player" ></div> | |
| <script> | |
| var youTubeAPILoaded = false; | |
| var videoContainer = document.getElementById("player"); | |
| function loadYouTubeAPI (callBack) { | |
| if (!youTubeAPILoaded) { | |
| var tag = document.createElement('script'); | |
| tag.src = "https://www.youtube.com/player_api"; | |
| var firstScriptTag = document.getElementsByTagName('script')[0]; | |
| firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
| window.onYouTubePlayerAPIReady = function () { | |
| youTubeAPILoaded = true; | |
| callBack(); | |
| } | |
| } else { | |
| callBack(); | |
| } | |
| } | |
| function showVideoByID (domElement, videoID,start) { | |
| var end = start + 5; | |
| loadYouTubeAPI(function () { | |
| if (!domElement.player) { | |
| domElement.player = new YT.Player(domElement, { | |
| height : '283', | |
| width : '600', | |
| videoId : videoID, | |
| playerVars: { | |
| 'rel' : 0, | |
| 'autoplay' : 1, | |
| 'controls' :0, | |
| 'disablekb':1, | |
| 'cc_load_policy':0, | |
| 'iv_load_policy':0, | |
| 'modestbranding':0, | |
| 'showinfo':0, | |
| 'start':start, | |
| 'end':end | |
| }, | |
| events: { | |
| 'onReady': onPlayerReady, | |
| 'onStateChange': onPlayerStateChange | |
| } | |
| }); | |
| } else { | |
| domElement.player.loadVideoById(videoID); | |
| } | |
| nowPlaying = player; | |
| currentPopup[0].previousLanguage = language | |
| }); | |
| } | |
| // end of "mini library" | |
| var youtubeReady = false; | |
| var videoReady = false; | |
| function onPlayerReady(event){ | |
| } | |
| function onPlayerStateChange(){ | |
| if(parseInt(event.data) == 1){ | |
| videoContainer.player.setVolume(0); | |
| volumeInterval = setInterval(setVolumeVideo,100); | |
| } | |
| } | |
| showVideoByID(videoContainer , "p2v1GIDmeNU", 90);// init | |
| videoContainer.player.stopVideo(); // stop video | |
| // fade in volume | |
| var newvolume = 0; | |
| var volumeInterval; | |
| function setVolumeVideo(){ | |
| if(newvolume <100){ | |
| newvolume += 5; | |
| videoContainer.player.setVolume(newvolume); | |
| }else{ | |
| clearInterval(volumeInterval); | |
| } | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment