Advertisement
caLLowCreation

YouTube Clip Player

Jul 16th, 2020
2,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.93 KB | None | 0 0
  1. <html lang="en">
  2.  
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>YouTube-Clip</title>
  6.     <script src="/socket.io/socket.io.js"></script>
  7.     <script src="https://code.jquery.com/jquery-3.2.1.min.js"
  8.         integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
  9.         </script>
  10.     <style>
  11.         #youtube-clip {
  12.             top: 50%;
  13.             left: 50%;
  14.             width: 400px;
  15.             height: 225px;
  16.             margin-top: -9em;
  17.             margin-left: -15em;
  18.             border: 1px solid #ccc;
  19.             background-color: #f3f3f3;
  20.             position: fixed;
  21.         }
  22.     </style>
  23. </head>
  24.  
  25. <body style="background-color: transparent">
  26.     <div id="youtube-clip" style="display: none;"></div>
  27. </body>
  28. <script>
  29.     'use strict';
  30.     //https://www.youtube.com/watch?v=b9XNyeeJZ2k
  31.     const socket = io.connect();
  32.     const NORMALIZE_VOLUME = 0.25;
  33.     const player = {
  34.         jdiv: null,
  35.         controller: null
  36.     };
  37.  
  38.     socket.on('connect', onConnect);
  39.     socket.on("disconnect", onDisconnect);
  40.  
  41.     socket.on('youtube-clip-play', playerPlay);
  42.     socket.on('youtube-clip-stop-hide', playerStopHide);
  43.  
  44.     // TODO: make clip volume command
  45.     socket.on('youtube-clip-volume', playerVolume);
  46.  
  47.     socket.on('youtube-clip-is-player-playing', playerIsPlayerPlaying);
  48.  
  49.     // TODO: make clip watch command
  50.     socket.on('youtube-clip-watch', playerWatch);
  51.  
  52.     // Load the IFrame Player API code asynchronously.
  53.     const tag = document.createElement('script');
  54.     tag.src = "https://www.youtube.com/player_api";
  55.     const firstScriptTag = document.getElementsByTagName('script')[0];
  56.     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  57.  
  58.     function onConnect() {
  59.         console.log("client Connected to server");
  60.     }
  61.  
  62.     function onDisconnect() {
  63.         console.log("client disconnected from server");
  64.     }
  65.  
  66.     // Replace the 'youtube-clip' element with an <iframe> and
  67.     // YouTube player after the API code downloads.
  68.     function onYouTubePlayerAPIReady() { //onYouTubeIframeAPIReady()
  69.         player.controller = new YT.Player('youtube-clip', {
  70.             width: '400',
  71.             height: '225',
  72.             videoId: 'zz6BaNiPkgY',
  73.             events: {
  74.                 'onReady': onPlayerReady,
  75.                 'onStateChange': onPlayerStateChange,
  76.                 'onError': onPlayerError
  77.             }
  78.         });
  79.     }
  80.  
  81.     function playerWatch(data) {
  82.         if (data.watch) {
  83.             player.jdiv.show();
  84.         } else {
  85.             player.jdiv.hide();
  86.         }
  87.     }
  88.  
  89.     function onPlayerReady(event) {
  90.         //event.target.setPlaybackQuality('144p');
  91.         player.controller = event.target;
  92.         player.jdiv = $('#youtube-clip');
  93.         playerStopHide();
  94.  
  95.         socket.emit('youtube-clip-ready');
  96.  
  97.     }
  98.  
  99.     function onPlayerStateChange(event) {
  100.  
  101.         /*if (event.data == YT.PlayerState.BUFFERING) {
  102.             event.target.setPlaybackQuality('144p');
  103.         }*/
  104.  
  105.         if (player.controller.getPlayerState() === YT.PlayerState.PLAYING/*playing*/) {
  106.             playerWatch(player.options);
  107.             return;
  108.         } else if (player.controller.getPlayerState() === YT.PlayerState.ENDED/*ended*/) {
  109.             player.jdiv.hide();
  110.             socket.emit('youtube-clip-ended');
  111.         } else {
  112.             // console.log(player.controller.getPlayerState());
  113.         }
  114.     }
  115.  
  116.     function onPlayerError(event) {
  117.  
  118.     }
  119.  
  120.     function playerPlayClip(data) {
  121.     }
  122.  
  123.     function playerPlay(options) {
  124.         player.controller.setPlaybackQuality(options.quality);
  125.         player.controller.loadVideoById({
  126.             videoId: options.parameters.videoId,
  127.             startSeconds: options.parameters.startSeconds,
  128.             endSeconds: options.parameters.endSeconds
  129.         });
  130.         player.controller.playVideo();
  131.         player.options = options;
  132.         playerVolume(options);
  133.  
  134.         console.log('youtube-clip-play', options);
  135.     }
  136.  
  137.     function playerVolume(options) {
  138.         const volume = options.volume * NORMALIZE_VOLUME;
  139.         player.controller.setVolume(volume);
  140.     }
  141.  
  142.     function playerStopHide() {
  143.         player.controller.stopVideo();
  144.         player.jdiv.hide();
  145.     }
  146.  
  147.     function playerIsPlayerPlaying() {
  148.         socket.emit('youtube-clip-is-player-playing', {
  149.             is_playing: (player.controller.getPlayerState() !== YT.PlayerState.ENDED && (player.jdiv.not(':hidden').length === YT.PlayerState.PLAYING)),
  150.             options: player.options
  151.         }, data => logCallback('youtube-clip-is-player-playing', data));
  152.     }
  153.  
  154.     function msTimes() {
  155.         const options = player.options;
  156.  
  157.         let duration = 0;
  158.         let position = 0;
  159.         let remaining = 0;
  160.         let item_id = '';
  161.         try {
  162.             duration = options.duration_ms / 1000; //options. player.controller.getDuration();
  163.             position = (options.parameters.startSeconds / 1000) + player.controller.getCurrentTime();
  164.             remaining = duration - position;
  165.             item_id = player.controller.getVideoData().video_id;
  166.         } catch (error) {
  167.             duration = 0;
  168.             position = 0;
  169.             remaining = 0;
  170.             item_id = player.controller.getVideoData().video_id;
  171.         }
  172.         return {
  173.             duration_ms: duration * 1000,
  174.             position_ms: position * 1000,
  175.             remaining_ms: remaining * 1000,
  176.             is_playing: player.controller.getPlayerState() === YT.PlayerState.PLAYING,
  177.             item_id: player.controller.getVideoData().video_id
  178.         }
  179.     }
  180.  
  181.     function logCallback(type, data) {
  182.         console.log(type, data);
  183.     }
  184.  
  185.     $(document).ready(() => {
  186.  
  187.     });
  188. </script>
  189.  
  190. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement