A web music player written in JS

player.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. function Player(sources) {
  2. this.playlist = sources;
  3. this.current = {};
  4. this.volume = 100;
  5. this.currentSong = -1;
  6. this.isPlaying = false;
  7. this.isMute = false;
  8. this.rate = 500;
  9. this.YTplayer = false;
  10. this.controls = {};
  11. this.interval = null;
  12. this.init = function() {
  13. this.loadYTplayer();
  14. this.bindControls();
  15. this.bindEvents();
  16. }
  17. this.bindEvents = function() {
  18. this.controls.toggle.addEventListener('click', this.toggle);
  19. this.controls.previous.addEventListener('click', this.previous);
  20. this.controls.next.addEventListener('click', this.next);
  21. this.controls.bar.addEventListener('click', this.seekPosition);
  22. this.controls.volume.addEventListener('input', this.setVolume);
  23. this.controls.volumeMute.addEventListener('click', this.setMute);
  24. }
  25. this.bindControls = function() {
  26. this.controls.toggle = document.getElementById('player-toggle');
  27. this.controls.next = document.getElementById('player-next');
  28. this.controls.previous = document.getElementById('player-previous');
  29. this.controls.volume = document.getElementById('player-volume-bar');
  30. this.controls.volumeSet = document.getElementById('player-volume-set');
  31. this.controls.volumeMute = document.getElementById('player-volume-mute');
  32. this.controls.bar = document.getElementById('player-bar');
  33. this.controls.buffer = document.getElementById('player-buffer');
  34. this.controls.played = document.getElementById('player-played');
  35. this.controls.cursor = document.getElementById('player-cursor');
  36. this.controls.duration = document.getElementById('player-duration');
  37. this.controls.currentTime = document.getElementById('player-currentTime');
  38. this.controls.title = document.getElementById('player-title');
  39. }
  40. this.getVideoID = function(url) {
  41. re = /\?v=([^&]*)/;
  42. match = re.exec(url);
  43. return (match && match.length >= 2 ? match[1] : '');
  44. }
  45. this.newSong = function (url) {
  46. if (!this.YTplayer) {
  47. this.YTplayer = new YT.Player('YTplayer', {
  48. height: '0',
  49. width: '0',
  50. videoId: this.getVideoID(url),
  51. events: {
  52. 'onReady': onPlayerReady,
  53. 'onStateChange': onPlayerStateChange
  54. }
  55. });
  56. } else {
  57. this.YTplayer.loadVideoById(this.getVideoID(url));
  58. }
  59. this.isPlaying = true;
  60. }
  61. this.loadYTplayer = function() {
  62. var tag = document.createElement('script');
  63. tag.src = "https://www.youtube.com/iframe_api";
  64. var firstScriptTag = document.getElementsByTagName('script')[0];
  65. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  66. };
  67. this.getState = function() {
  68. var current = this.YTplayer.getCurrentTime();
  69. var bufferized = this.YTplayer.getVideoLoadedFraction() * 100;
  70. var duration = this.current.duration;
  71. var fraction = (current / duration) * 100;
  72. this.controls.played.style.width = fraction + '%';
  73. this.controls.buffer.style.width = bufferized + '%';
  74. this.controls.cursor.style.left = fraction + '%';
  75. current = new Date(current * 1000);
  76. current = current.toTimeString().substr(3, 6);
  77. this.controls.currentTime.textContent = current;
  78. }
  79. this.seekPosition = function() {
  80. var duration = evaplayer.current.duration;
  81. var fraction = (event.offsetX / evaplayer.controls.bar.clientWidth) * 100;
  82. var timeseeked = (duration * fraction) / 100;
  83. evaplayer.YTplayer.seekTo(timeseeked);
  84. };
  85. this.setVolume = function() {
  86. evaplayer.YTplayer.setVolume(event.target.value);
  87. evaplayer.volume = event.target.value;
  88. };
  89. this.setMute = function() {
  90. if (!evaplayer.isMute) {
  91. evaplayer.YTplayer.mute();
  92. evaplayer.controls.volume.value = 0;
  93. evaplayer.controls.volumeMute.style.textDecoration = 'line-through';
  94. } else {
  95. evaplayer.YTplayer.unMute();
  96. evaplayer.controls.volume.value = evaplayer.volume;
  97. evaplayer.controls.volumeMute.style.textDecoration = 'none';
  98. }
  99. evaplayer.isMute = !evaplayer.isMute;
  100. };
  101. this.toggle = function() {
  102. if (evaplayer.isPlaying)
  103. evaplayer.YTplayer.pauseVideo();
  104. else
  105. evaplayer.YTplayer.playVideo();
  106. evaplayer.isPlaying = !evaplayer.isPlaying;
  107. return evaplayer;
  108. };
  109. this.next = function() {
  110. evaplayer.currentSong++;
  111. if (evaplayer.currentSong > evaplayer.playlist.length - 1)
  112. evaplayer.currentSong = 0;
  113. evaplayer.newSong(evaplayer.playlist[evaplayer.currentSong].link);
  114. };
  115. this.previous = function() {
  116. evaplayer.currentSong--;
  117. if (evaplayer.currentSong < 0)
  118. evaplayer.currentSong = evaplayer.playlist.length - 1;
  119. evaplayer.newSong(evaplayer.playlist[evaplayer.currentSong].link);
  120. };
  121. this.updateInfo = function() {
  122. var data = this.YTplayer.getVideoData();
  123. var duration = this.YTplayer.getDuration();
  124. this.current = {
  125. author: data.author,
  126. title: data.title,
  127. duration: duration
  128. };
  129. var title = this.current.title;
  130. if (this.current.author)
  131. title = this.current.author + ' - ' + title;
  132. this.controls.title.textContent = title;
  133. var duration = this.current.duration;
  134. duration = new Date(duration * 1000);
  135. duration = duration.toTimeString().substr(3, 6);
  136. this.controls.duration.textContent = duration;
  137. };
  138. function onPlayerReady(event) {
  139. event.target.playVideo();
  140. if (!evaplayer.interval)
  141. evaplayer.interval = setInterval(function() {
  142. evaplayer.getState();
  143. }, evaplayer.rate);
  144. }
  145. function onPlayerStateChange(event) {
  146. switch (event.data) {
  147. case YT.PlayerState.PLAYING:
  148. evaplayer.isPlaying = true;
  149. evaplayer.updateInfo();
  150. evaplayer.controls.toggle.textContent = '◻';
  151. if (!evaplayer.interval)
  152. evaplayer.interval = setInterval(function() {
  153. evaplayer.getState();
  154. }, evaplayer.rate);
  155. break;
  156. case YT.PlayerState.ENDED:
  157. evaplayer.next();
  158. break;
  159. case YT.PlayerState.PAUSED:
  160. evaplayer.isPlaying = false;
  161. evaplayer.controls.toggle.textContent = '▻';
  162. clearInterval(evaplayer.interval);
  163. evaplayer.interval = null;
  164. break;
  165. }
  166. }
  167. this.init();
  168. };
  169. function onYouTubeIframeAPIReady() {
  170. evaplayer.next();
  171. }