Advertisement
caLLowCreation

Twitch Clip Player

Jul 16th, 2020
3,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.59 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Twitch-Clip</title>
  7.     <script src="/socket.io/socket.io.js"></script>
  8.     <script src="https://code.jquery.com/jquery-3.2.1.min.js"
  9.         integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  10.     <script src="//player.twitch.tv/js/embed/v1.js"></script>
  11.     <style>
  12.         #twitch-clip{
  13.             top:50%;
  14.             left:50%;
  15.             width:400px;
  16.             height:225px;
  17.             margin-top:-9em;
  18.             margin-left:-15em;
  19.             border:1px solid #ccc;
  20.             background-color:#f3f3f3;
  21.             position:fixed;
  22.         }
  23.     </style>
  24. </head>
  25.  
  26. <body style="background-color: transparent">
  27.     <div id="twitch-clip" style="display: visible;">
  28.         <iframe width="400" height="225" frameborder="0" scrolling="no" allowfullscreen="false" parent="localhost">
  29.         </iframe>
  30.     </div>
  31. </body>
  32. <script>
  33.     'use strict';
  34.     //https://www.twitch.com/cosmos
  35.     const socket = io.connect();
  36.     const NORMALIZE_VOLUME = 1.0;
  37.     const player = {
  38.         jdiv: null,
  39.         jiframe: null,
  40.         controller: null
  41.     };
  42.  
  43.     let is_playing = false;
  44.  
  45.     socket.on('connect', onConnect);
  46.     socket.on("disconnect", onDisconnect);
  47.  
  48.     socket.on('twitch-clip-play', playerPlay);
  49.     socket.on('twitch-clip-stop-hide', playerStopHide);
  50.  
  51.     // TODO: make clip volume command
  52.     socket.on('twitch-clip-volume', playerVolume);
  53.  
  54.     socket.on('twitch-clip-is-player-playing', playerIsPlayerPlaying);
  55.  
  56.     function onConnect() {
  57.         console.log("client Connected to server");
  58.     }
  59.  
  60.     function onDisconnect() {
  61.         console.log("client disconnected from server");
  62.     }
  63.  
  64.     // Replace the 'twitch-player' element with an <iframe> and
  65.     // call in $document ready to init player.
  66.     function onTwitchAPIReady() {
  67.         player.controller = new Twitch.Player('twitch-player', {
  68.             width: '400',
  69.             height: '225',
  70.             channel: 'cosmos',
  71.             autoplay: true
  72.         });
  73.  
  74.         player.controller.addEventListener(Twitch.Player.READY, onPlayerReady);
  75.         player.controller.addEventListener(Twitch.Player.PLAYING, onPlayerStateChange);
  76.         player.controller.addEventListener(Twitch.Player.ENDED, onPlayerStateChange);
  77.     }
  78.  
  79.     function onPlayerReady() {
  80.         player.jdiv = $('#twitch-clip');
  81.         playerStopHide();
  82.         socket.emit('twitch-clip-ready');
  83.     }
  84.  
  85.     function onPlayerStateChange(event) {
  86.         if (player.controller.isPaused() !== true/*playing*/) {
  87.             return;
  88.         } else if (player.controller.getEnded() === true/*ended*/) {
  89.             player.jdiv.hide();
  90.             socket.emit('twitch-clip-ended');
  91.         } else {
  92.             // console.log(player.controller.getPlaybackStats()); // (Deprecated) Returns an object with statistics on the embedded video player and the current live stream or VOD. The format of the returned object is undefined.
  93.         }
  94.     }
  95.  
  96.     function playerPlay(options) {
  97.         $('#twitch-clip iframe').attr({ src: `https://clips.twitch.tv/embed?clip=${options.parameters.videoId}&parent=${options.parameters.parent}` });
  98.         player.jdiv.show();
  99.         player.options = options;
  100.         is_playing = true;
  101.     }
  102.  
  103.     function playerVolume(options) {
  104.         const volume = options.volume * NORMALIZE_VOLUME;
  105.         player.controller.setVolume(volume * 0.01);
  106.     }
  107.  
  108.     function playerStopHide() {
  109.         $('#twitch-clip iframe').attr({ src: '' });
  110.         player.jdiv.hide();
  111.         is_playing = false;
  112.     }
  113.  
  114.     function playerIsPlayerPlaying() {
  115.         socket.emit('twitch-clip-is-player-playing', {
  116.             is_playing: is_playing,
  117.             options: player.options
  118.         }, (data) => {
  119.             // console.log(data); // data will be 'woot'
  120.         });
  121.     }
  122.  
  123.     $(document).ready(() => {
  124.         onPlayerReady();
  125.         ///$('#twitch-clip-player iframe').attr({src: 'https://clips.twitch.tv/embed?clip=AnimatedSpunkyEchidnaTakeNRG&parent=localhost'});
  126.     });
  127. </script>
  128.  
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement