player.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { Playlist } from './playlist.js'
  2. let baseClass = "button is-small is-light"
  3. let clactive = baseClass + " is-link"
  4. let currentPlaylist = []
  5. class MainPlayer {
  6. constructor() {
  7. this.playlist = new Playlist()
  8. this.config = {
  9. currentIndex: 0,
  10. loop: () => localStorage.getItem('loop'),
  11. }
  12. this.button = {
  13. play: document.getElementById("play"),
  14. pause: document.getElementById("pause"),
  15. loop: document.getElementById("loop")
  16. }
  17. this.controls = {
  18. play: (elem, act) => {
  19. window.videoPlayer.play()
  20. if (!act) {
  21. elem.className = clactive
  22. this.button.pause.className = baseClass
  23. this.button.pause.setAttribute('active', "false")
  24. } else {
  25. elem.className = baseClass
  26. document.getElementById("pause").className = baseClass
  27. }
  28. },
  29. pause: (elem, act) => {
  30. window.videoPlayer.pause()
  31. if (!act) {
  32. elem.className = clactive
  33. this.button.play.className = baseClass
  34. this.button.play.setAttribute('active', "false")
  35. } else {
  36. elem.className = baseClass
  37. document.getElementById("play").className = clactive
  38. }
  39. },
  40. previous: (elem) => {
  41. },
  42. next: (elem) => {
  43. },
  44. loop: (elem, act) => {
  45. let swap = act ? false : true
  46. elem.setAttribute('active', swap)
  47. localStorage.setItem('loop', swap);
  48. console.log("swap", swap)
  49. if (swap) elem.className = clactive
  50. else elem.className = baseClass
  51. }
  52. }
  53. this.ctrlPlayer = (cmd, srcElem) => {
  54. let active = srcElem.getAttribute("active") == "false" ? false : true
  55. this.controls[cmd](srcElem, active)
  56. },
  57. this.addVideo = function () {
  58. const dialogConfig = {
  59. title: 'Select video files',
  60. buttonLabel: 'Validate Selection',
  61. filters: [
  62. { name: 'Movies', extensions: ['mkv', 'avi', 'mp4', "m4v", "webm", "ogv"] }
  63. ],
  64. properties: ['openFile', 'multiSelections']
  65. };
  66. electron.openDialog('showOpenDialog', dialogConfig)
  67. .then((result) => {
  68. window.videoPlayer.src = result?.filePaths[0]
  69. window.videoPlayer.play()
  70. createPlaylist(result.filePaths)
  71. })
  72. .catch((err) => { console.log("err", err) })
  73. }
  74. this.createPlaylist = function (list) {
  75. let playlist = document.getElementById('playlist')
  76. let emptyFlag = document.getElementById('emptyFlag')
  77. emptyFlag.style.display = "none"
  78. list?.forEach((source) => {
  79. currentPlaylist.push(source)
  80. localStorage.setItem('playlist', currentPlaylist.join("|"));
  81. let pathfull = source.split("/")
  82. let filename = pathfull[pathfull.length - 1]
  83. let videoNeo = document.createElement('li')
  84. let delButton = document.createElement('button')
  85. delButton.className = "delete is-small"
  86. delButton.addEventListener('dblclick', function (event) {
  87. console.log("db click")
  88. removeVideo(event.srcElement.parentNode)
  89. });
  90. videoNeo.innerHTML = `<span class="hand"> </span>${filename}`
  91. videoNeo.setAttribute("path", source);
  92. videoNeo.setAttribute("index", playlist.getElementsByTagName('li').length);
  93. videoNeo.appendChild(delButton)
  94. videoNeo.onclick = () => {
  95. videoPlayer.src = videoNeo.getAttribute('path')
  96. window.curVideo = videoNeo.getAttribute('index')
  97. window.videoPlayer.play()
  98. }
  99. playlist.appendChild(videoNeo)
  100. // console.log("window.playlist", window.playlist);
  101. })
  102. },
  103. this.removeVideo = function (val) {
  104. var emptyFlag = document.getElementById('emptyFlag')
  105. var listElements = document.getElementById("playlist");
  106. let sourcePath = val.getAttribute("path")
  107. let sourceIndex = currentPlaylist.indexOf(sourcePath)
  108. listElements.removeChild(val);
  109. if (sourceIndex !== -1) {
  110. currentPlaylist.splice(sourceIndex, 1);
  111. }
  112. // If playlist is empty
  113. if (listElements.childElementCount == 1) {
  114. emptyFlag.style.display = "flex"
  115. window.videoPlayer.pause()
  116. window.videoPlayer.src = "./assets/preview.mp4";
  117. window.videoPlayer.play()
  118. }
  119. //update playlist cache
  120. localStorage.setItem('playlist', currentPlaylist.join("|"));
  121. },
  122. this.initPlayer = function() {
  123. window.videoPlayer = document.createElement('video');
  124. window.videoPlayer.id = "window.videoPlayerplayer"
  125. window.videoPlayer.width = 1024;
  126. window.videoPlayer.height = 640;
  127. window.videoPlayer.loop = false;
  128. window.videoPlayer.muted = true;
  129. window.videoPlayer.src = "./assets/preview.mp4";
  130. window.videoPlayer.setAttribute('webkit-playsinline', 'webkit-playsinline');
  131. window.videoPlayer.setAttribute('playsinline', 'playsinline');
  132. window.videoPlayer.setAttribute('crossorigin', 'anonymous');
  133. window.videoPlayer.play();
  134. window.curVideo = 0;
  135. let self = this
  136. window.videoPlayer.onended = function () {
  137. let playlistVideos = window.playlist.el.getElementsByTagName("li")
  138. let looping = self.config.loop()
  139. if (playlistVideos.length !== 0) {
  140. window.curVideo++;
  141. console.log("looping", looping)
  142. console.log("window.curVideo", window.curVideo, playlistVideos.length, window.curVideo < playlistVideos.length - 1)
  143. // go to next video in the playlist
  144. if (window.curVideo < playlistVideos.length) {
  145. // console.log("try to switch no next video", playlistVideos[window.curVideo]);
  146. window.videoPlayer.src = playlistVideos[window.curVideo].getAttribute('path');
  147. window.videoPlayer.play()
  148. }
  149. // last video of the playlist loop back to the begining.
  150. else if (window.curVideo >= playlistVideos.length - 1&& looping == "true") {
  151. console.log("is looping ? !", looping)
  152. window.curVideo = 0;
  153. window.videoPlayer.src = playlistVideos[window.curVideo].getAttribute('path');
  154. window.videoPlayer.play()
  155. } else {
  156. console.log("playlist end")
  157. window.curVideo = 0
  158. }
  159. } else {
  160. window.curVideo = 0
  161. window.videoPlayer.play()
  162. }
  163. }
  164. console.log("init loop", this.config.loop())
  165. this.button.loop.setAttribute('active', this.config.loop() ? "true" : "false")
  166. console.log("test loop", this.config.loop() == "true" ? clactive : baseClass)
  167. this.button.loop.className = ""
  168. this.button.loop.className = this.config.loop() == "true" ? clactive : baseClass
  169. console.log("className = ", this.button.loop.className)
  170. }
  171. }
  172. }
  173. export { MainPlayer };