Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div id="player"><iframe width="300" height="200" src="https://www.youtube.com/embed/url-id-video" frameborder="0" allowfullscreen></iframe></div>
- <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: '200',
- width: '300',
- videoId: 'url-id-video',
- events: {
- 'onReady': onPlayerReady,
- 'onStateChange': onPlayerStateChange
- }
- });
- }
- // 4. The API will call this function when the video player is ready.
- // In "event.target.setVolume (0)" is with audio on mute (value: 0).
- // To put maximum volume change to "event.target.setVolume (100)"
- function onPlayerReady(event) {
- event.target.playVideo();
- event.target.setVolume(0);
- }
- // 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 two minutes and then stop.
- // To increase or decrease the time just change the value 120000, which is equivalent to two minutes.
- // Code edited by limontec.com
- var done = false;
- function onPlayerStateChange(event) {
- if (event.data == YT.PlayerState.PLAYING && !done) {
- setTimeout(stopVideo, 120000);
- done = true;
- }
- }
- function stopVideo() {
- player.stopVideo();
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement