Station.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <station-header></station-header>
  3. <div class="station">
  4. <div class="row">
  5. <div class="col-md-8 col-md-push-2 col-sm-10 col-sm-push-1 col-xs-12 video-col">
  6. <div class="video-container">
  7. <div id="player"></div>
  8. </div>
  9. </div>
  10. <div class="col-md-8 col-md-push-2 col-sm-10 col-sm-push-1 col-xs-12">
  11. <div class="row">
  12. <!--<button v-if="paused" @click="unpauseStation()">Unpause</button>-->
  13. <!--<button v-if="!paused" @click="pauseStation()">Pause</button>-->
  14. <div class="col-md-8 col-sm-12 col-sm-12">
  15. <h4 id="time-display">{{timeElapsed}} / {{currentSong.duration}}</h4>
  16. <h3>{{currentSong.title}}</h3>
  17. <h4 class="thin" style="margin-left: 0">{{currentSong.artists}}</h4>
  18. <div class="row">
  19. <form style="margin-top: 12px; margin-bottom: 0;" action="#" class="col-md-4 col-lg-4 col-xs-4 col-sm-4">
  20. <p style="margin-top: 0; position: relative;">
  21. <input type="range" id="volumeSlider" min="0" max="100" class="active" v-on:change="changeVolume()" v-on:input="changeVolume()">
  22. </p>
  23. </form>
  24. <div class="col-xs-8 col-sm-5 col-md-5" style="float: right;">
  25. <ul id="ratings">
  26. <li id="like" class="right"><span class="flow-text">{{currentSong.likes}} </span> <i id="thumbs_up" class="material-icons grey-text">thumb_up</i></li>
  27. <li style="margin-right: 10px;" id="dislike" class="right"><span class="flow-text">{{currentSong.dislikes}} </span><i id="thumbs_down" class="material-icons grey-text">thumb_down</i></li>
  28. </ul>
  29. </div>
  30. </div>
  31. <div class="seeker-bar-container white" id="preview-progress">
  32. <div class="seeker-bar light-blue" style="width: 60.9869%;"></div>
  33. </div>
  34. </div>
  35. <img class="img-responsive col-md-4 col-xs-12 col-sm-12" id="song-thumbnail" style="margin-top: 10px !important" :src="currentSong.thumbnail" alt="Song Thumbnail" />
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. <main-footer></main-footer>
  41. <div class="modal fade" id="queue" tabindex="-1" role="dialog" aria-labelledby="queue-modal">
  42. <div class="modal-dialog modal-large" role="document">
  43. <div class="modal-content">
  44. <div class="modal-header">
  45. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  46. <h5 class="modal-title">Add to Station</h5>
  47. </div>
  48. <div class="modal-body">
  49. <div class="form-group">
  50. <div class="input-group">
  51. <input class="form-control" type="text" placeholder="YouTube Query" v-model="queryInput"/>
  52. <a type="button" class="input-group-btn btn btn-primary btn-search" @click="submitQuery()">Search</a>
  53. </div>
  54. </div>
  55. <div>
  56. <div v-for="result in queryResults">
  57. <div class="media">
  58. <div class="media-left">
  59. <a href={{result.url}}>
  60. <img class="media-object" :src="result.thumbnail" />
  61. </a>
  62. </div>
  63. <div class="media-body">
  64. <h4 class="media-heading">{{result.title}}</h4>
  65. <button class="btn btn-success" @click='addSongToQueue(result)'>Add</button>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. </template>
  75. <script>
  76. import StationHeader from '../StationHeader.vue'
  77. import MainFooter from '../MainFooter.vue'
  78. export default {
  79. data() {
  80. return {
  81. playerReady: false,
  82. currentSong: {},
  83. player: undefined,
  84. timePaused: 0,
  85. paused: false,
  86. timeElapsed: "0:00",
  87. interval: 0,
  88. queryInput: "",
  89. queryResults: [],
  90. queue: []
  91. }
  92. },
  93. methods: {
  94. youtubeReady: function() {
  95. let local = this;
  96. local.player = new YT.Player("player", {
  97. height: 270,
  98. width: 480,
  99. videoId: local.currentSong.id,
  100. playerVars: {controls: 1, iv_load_policy: 3, rel: 0, showinfo: 0},
  101. events: {
  102. 'onReady': function(event) {
  103. local.playerReady = true;
  104. let volume = parseInt(localStorage.getItem("volume"));
  105. volume = (typeof volume === "number") ? volume : 20;
  106. local.player.setVolume(volume);
  107. if (volume > 0) {
  108. local.player.unMute();
  109. }
  110. local.playVideo();
  111. },
  112. 'onStateChange': function(event) {
  113. if (event.data === 1 && local.videoLoading === true) {
  114. local.videoLoading = false;
  115. local.player.seekTo(local.getTimeElapsed() / 1000, true);
  116. if (local.paused) {
  117. local.player.pauseVideo();
  118. }
  119. }
  120. }
  121. }
  122. });
  123. },
  124. getTimeElapsed: function() {
  125. let local = this;
  126. if (local.currentSong !== undefined) {
  127. return Date.now() - local.currentSong.startedAt - local.timePaused;
  128. } else {
  129. return 0;
  130. }
  131. },
  132. playVideo: function() {
  133. let local = this;
  134. if (local.playerReady) {
  135. local.videoLoading = true;
  136. local.player.loadVideoById(local.currentSong.id);
  137. const d = moment.duration(parseInt(local.currentSong.duration), 'seconds');
  138. local.currentSong.songDuration = d.minutes() + ":" + ("0" + d.seconds()).slice(-2);
  139. local.currentSong.artists = local.currentSong.artists.join(", ");
  140. if (local.interval !== 0) {
  141. clearInterval(local.interval);
  142. }
  143. local.interval = setInterval(function () {
  144. local.resizeSeekerbar();
  145. local.calculateTimeElapsed();
  146. }, 250);
  147. }
  148. },
  149. resizeSeekerbar: function() {
  150. let local = this;
  151. if (!local.paused) {
  152. $(".seeker-bar").width(((local.getTimeElapsed() / 1000) / local.currentSong.duration * 100) + "%");
  153. }
  154. },
  155. calculateTimeElapsed: function() {
  156. let local = this;
  157. let currentTime = Date.now();
  158. if (local.currentTime !== undefined && local.paused) {
  159. local.timePaused += (Date.now() - local.currentTime);
  160. local.currentTime = undefined;
  161. }
  162. let duration = (Date.now() - local.currentSong.startedAt - local.timePaused) / 1000;
  163. let songDuration = local.currentSong.duration;
  164. if (songDuration <= duration) {
  165. local.player.pauseVideo();
  166. }
  167. let d = moment.duration(duration, 'seconds');
  168. if ((!local.paused || local.timeElapsed === "0:00") && duration <= songDuration) {
  169. local.timeElapsed = d.minutes() + ":" + ("0" + d.seconds()).slice(-2);
  170. }
  171. },
  172. changeVolume: function() {
  173. let local = this;
  174. let volume = $("#volumeSlider").val();
  175. localStorage.setItem("volume", volume);
  176. if (local.playerReady) {
  177. local.player.setVolume(volume);
  178. if (volume > 0) {
  179. local.player.unMute();
  180. }
  181. }
  182. },
  183. unpauseStation: function() {
  184. let local = this;
  185. local.paused = false;
  186. if (local.playerReady) {
  187. local.player.seekTo(local.getTimeElapsed() / 1000);
  188. local.player.playVideo();
  189. }
  190. },
  191. pauseStation: function() {
  192. let local = this;
  193. local.paused = true;
  194. if (local.playerReady) {
  195. local.player.pauseVideo();
  196. }
  197. },
  198. addSongToQueue: function(song) {
  199. let local = this;
  200. local.socket.emit("/songs/queue/add/:song", song, function(data) {
  201. console.log(data);
  202. });
  203. },
  204. submitQuery: function() {
  205. let local = this;
  206. local.socket.emit("/youtube/getVideo/:query", local.queryInput, function(data) {
  207. local.queryResults = [];
  208. for (let i = 0; i < data.items.length; i++) {
  209. local.queryResults.push({
  210. id: data.items[i].id.videoId,
  211. url: `https://www.youtube.com/watch?v=${this.id}`,
  212. title: data.items[i].snippet.title,
  213. thumbnail: data.items[i].snippet.thumbnails.default.url
  214. });
  215. }
  216. });
  217. }
  218. },
  219. ready: function() {
  220. let local = this;
  221. local.interval = 0;
  222. local.socket = this.$parent.socket;
  223. local.stationSocket = io.connect(`http://dev.musare.com/${local.$route.params.id}`);
  224. local.stationSocket.on("connected", function(data) {
  225. local.currentSong = data.currentSong;
  226. local.paused = data.paused;
  227. local.timePaused = data.timePaused;
  228. local.currentTime = data.currentTime;
  229. });
  230. local.youtubeReady();
  231. local.stationSocket.on("nextSong", function(currentSong) {
  232. local.currentSong = currentSong;
  233. local.timePaused = 0;
  234. local.playVideo();
  235. });
  236. let volume = parseInt(localStorage.getItem("volume"));
  237. volume = (typeof volume === "number") ? volume : 20;
  238. $("#volumeSlider").val(volume);
  239. },
  240. components: { StationHeader, MainFooter }
  241. }
  242. </script>
  243. <style lang="sass">
  244. .modal-large {
  245. width: 75%;
  246. }
  247. .station {
  248. flex: 1 0 auto;
  249. padding-top: 4.5vw;
  250. transition: all 0.1s;
  251. margin: 0 auto;
  252. max-width: 1280px;
  253. width: 90%;
  254. @media only screen and (min-width: 993px) {
  255. width: 70%;
  256. }
  257. @media only screen and (min-width: 601px) {
  258. width: 85%;
  259. }
  260. input[type=range] {
  261. -webkit-appearance: none;
  262. width: 100%;
  263. margin: 7.3px 0;
  264. }
  265. input[type=range]:focus {
  266. outline: none;
  267. }
  268. input[type=range]::-webkit-slider-runnable-track {
  269. width: 100%;
  270. height: 5.2px;
  271. cursor: pointer;
  272. box-shadow: 0;
  273. background: #c2c0c2;
  274. border-radius: 0;
  275. border: 0;
  276. }
  277. input[type=range]::-webkit-slider-thumb {
  278. box-shadow: 0;
  279. border: 0;
  280. height: 19px;
  281. width: 19px;
  282. border-radius: 15px;
  283. background: #03a9f4;
  284. cursor: pointer;
  285. -webkit-appearance: none;
  286. margin-top: -6.5px;
  287. }
  288. input[type=range]::-moz-range-track {
  289. width: 100%;
  290. height: 5.2px;
  291. cursor: pointer;
  292. box-shadow: 0;
  293. background: #c2c0c2;
  294. border-radius: 0;
  295. border: 0;
  296. }
  297. input[type=range]::-moz-range-thumb {
  298. box-shadow: 0;
  299. border: 0;
  300. height: 19px;
  301. width: 19px;
  302. border-radius: 15px;
  303. background: #03a9f4;
  304. cursor: pointer;
  305. -webkit-appearance: none;
  306. margin-top: -6.5px;
  307. }
  308. input[type=range]::-ms-track {
  309. width: 100%;
  310. height: 5.2px;
  311. cursor: pointer;
  312. box-shadow: 0;
  313. background: #c2c0c2;
  314. border-radius: 1.3px;
  315. }
  316. input[type=range]::-ms-fill-lower {
  317. background: #c2c0c2;
  318. border: 0;
  319. border-radius: 0;
  320. box-shadow: 0;
  321. }
  322. input[type=range]::-ms-fill-upper {
  323. background: #c2c0c2;
  324. border: 0;
  325. border-radius: 0;
  326. box-shadow: 0;
  327. }
  328. input[type=range]::-ms-thumb {
  329. box-shadow: 0;
  330. border: 0;
  331. height: 15px;
  332. width: 15px;
  333. border-radius: 15px;
  334. background: #03a9f4;
  335. cursor: pointer;
  336. -webkit-appearance: none;
  337. margin-top: 1.5px;
  338. }
  339. .video-container {
  340. position: relative;
  341. padding-bottom: 56.25%;
  342. height: 0;
  343. overflow: hidden;
  344. iframe {
  345. position: absolute;
  346. top: 0;
  347. left: 0;
  348. width: 100%;
  349. height: 100%;
  350. pointer-events: none;
  351. }
  352. }
  353. .video-col {
  354. padding-right: 0.75rem;
  355. padding-left: 0.75rem;
  356. }
  357. }
  358. .room-title {
  359. left: 50%;
  360. -webkit-transform: translateX(-50%);
  361. transform: translateX(-50%);
  362. font-size: 2.1em;
  363. }
  364. #ratings {
  365. span {
  366. font-size: 1.68rem;
  367. }
  368. i {
  369. color: #9e9e9e !important;
  370. cursor: pointer;
  371. transition: 0.1s color;
  372. }
  373. }
  374. #time-display {
  375. margin-top: 30px;
  376. float: right;
  377. }
  378. #thumbs_up:hover {
  379. color: #87D37C !important;
  380. }
  381. #thumbs_down:hover {
  382. color: #EC644B !important;
  383. }
  384. .seeker-bar-container {
  385. position: relative;
  386. height: 5px;
  387. display: block;
  388. width: 100%;
  389. overflow: hidden;
  390. margin-top: 20px;
  391. }
  392. .seeker-bar {
  393. top: 0;
  394. left: 0;
  395. bottom: 0;
  396. position: absolute;
  397. }
  398. ul {
  399. list-style: none;
  400. margin: 0;
  401. display: block;
  402. }
  403. h1, h2, h3, h4, h5, h6 {
  404. font-weight: 400;
  405. line-height: 1.1;
  406. }
  407. h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {
  408. font-weight: inherit;
  409. }
  410. h1 {
  411. font-size: 4.2rem;
  412. line-height: 110%;
  413. margin: 2.1rem 0 1.68rem 0;
  414. }
  415. h2 {
  416. font-size: 3.56rem;
  417. line-height: 110%;
  418. margin: 1.78rem 0 1.424rem 0;
  419. }
  420. h3 {
  421. font-size: 2.92rem;
  422. line-height: 110%;
  423. margin: 1.46rem 0 1.168rem 0;
  424. }
  425. h4 {
  426. font-size: 2.28rem;
  427. line-height: 110%;
  428. margin: 1.14rem 0 0.912rem 0;
  429. }
  430. h5 {
  431. font-size: 1.64rem;
  432. line-height: 110%;
  433. margin: 0.82rem 0 0.656rem 0;
  434. }
  435. h6 {
  436. font-size: 1rem;
  437. line-height: 110%;
  438. margin: 0.5rem 0 0.4rem 0;
  439. }
  440. .thin {
  441. font-weight: 200;
  442. }
  443. .left {
  444. float: left !important;
  445. }
  446. .right {
  447. float: right !important;
  448. }
  449. .light-blue {
  450. background-color: #03a9f4 !important;
  451. }
  452. .white {
  453. background-color: #FFFFFF !important;
  454. }
  455. .btn-search {
  456. font-size: 14px;
  457. }
  458. </style>