koel/resources/assets/js/services/playback.js

398 lines
10 KiB
JavaScript
Raw Normal View History

import { shuffle, orderBy } from 'lodash';
2015-12-13 04:42:28 +00:00
import $ from 'jquery';
2016-04-05 07:38:10 +00:00
import plyr from 'plyr';
2016-06-25 05:24:55 +00:00
import Vue from 'vue';
2015-12-13 04:42:28 +00:00
2016-06-25 05:24:55 +00:00
import { event, loadMainView } from '../utils';
2015-12-13 04:42:28 +00:00
import queueStore from '../stores/queue';
import songStore from '../stores/song';
import artistStore from '../stores/artist';
2016-04-05 09:19:20 +00:00
import preferences from '../stores/preference';
2015-12-13 04:42:28 +00:00
import config from '../config';
export default {
player: null,
$volumeInput: null,
repeatModes: ['NO_REPEAT', 'REPEAT_ALL', 'REPEAT_ONE'],
2016-01-03 08:09:34 +00:00
initialized: false,
2015-12-13 04:42:28 +00:00
/**
* Initialize the playback service for this whole Koel app.
*
2016-01-17 14:26:24 +00:00
* @param {Vue} app The root Vue component.
2015-12-13 04:42:28 +00:00
*/
2016-06-25 05:24:55 +00:00
init() {
2016-01-03 08:09:34 +00:00
// We don't need to init this service twice, or the media events will be duplicated.
if (this.initialized) {
return;
}
this.player = plyr.setup({
2015-12-13 04:42:28 +00:00
controls: [],
})[0];
2015-12-13 04:42:28 +00:00
this.audio = $('audio');
2015-12-13 04:42:28 +00:00
this.$volumeInput = $('#volumeRange');
/**
* Listen to 'error' event on the audio player and play the next song if any.
*/
document.querySelector('.plyr').addEventListener('error', e => {
2015-12-13 04:42:28 +00:00
this.playNext();
2016-01-30 15:14:15 +00:00
}, true);
2015-12-13 04:42:28 +00:00
/**
* Listen to 'ended' event on the audio player and play the next song in the queue.
2015-12-13 04:42:28 +00:00
*/
document.querySelector('.plyr').addEventListener('ended', e => {
2016-03-18 04:45:12 +00:00
songStore.scrobble(queueStore.current);
2016-04-05 09:19:20 +00:00
if (preferences.repeatMode === 'REPEAT_ONE') {
2016-01-25 07:21:00 +00:00
this.restart();
2015-12-13 04:42:28 +00:00
return;
}
this.playNext();
});
/**
* Attempt to preload the next song if the current song is about to end.
*/
document.querySelector('.plyr').addEventListener('timeupdate', e => {
if (!this.player.media.duration || this.player.media.currentTime + 10 < this.player.media.duration) {
return;
}
// The current song has only 10 seconds left to play.
2016-03-28 13:38:14 +00:00
const nextSong = queueStore.next;
if (!nextSong || nextSong.preloaded) {
return;
}
2016-03-28 13:38:14 +00:00
const $preloader = $('<audio>');
$preloader.attr('src', songStore.getSourceUrl(nextSong));
nextSong.preloaded = true;
});
/**
* Listen to 'input' event on the volume range control.
* When user drags the volume control, this event will be triggered, and we
* update the volume on the plyr object.
*/
this.$volumeInput.on('input', e => {
this.setVolume($(e.target).val());
});
2015-12-13 04:42:28 +00:00
// On init, set the volume to the value found in the local storage.
2016-04-05 09:19:20 +00:00
this.setVolume(preferences.volume);
2016-01-03 08:09:34 +00:00
2016-01-11 15:25:58 +00:00
// Init the equalizer if supported.
2016-06-25 05:24:55 +00:00
event.emit('equalizer:init', this.player.media);
2016-01-11 15:25:58 +00:00
2016-01-03 08:09:34 +00:00
this.initialized = true;
2015-12-13 04:42:28 +00:00
},
/**
* Play a song. Because
*
* So many adventures couldn't happen today,
2015-12-13 04:42:28 +00:00
* So many songs we forgot to play
* So many dreams swinging out of the blue
* We'll let them come true
*
2016-01-06 16:41:59 +00:00
* @param {Object} song The song to play
2015-12-13 04:42:28 +00:00
*/
play(song) {
if (!song) {
return;
}
2016-03-18 04:45:12 +00:00
if (queueStore.current) {
queueStore.current.playbackState = 'stopped';
}
song.playbackState = 'playing';
2015-12-13 04:42:28 +00:00
// Set the song as the current song
2016-03-18 04:45:12 +00:00
queueStore.current = song;
// Add it into the "recent" list
songStore.addRecent(song);
// Manually set the `src` attribute of the audio to prevent plyr from resetting
// the audio media object and cause our equalizer to malfunction.
this.player.media.src = songStore.getSourceUrl(song);
2016-01-25 07:21:00 +00:00
2016-02-18 14:59:09 +00:00
$('title').text(`${song.title}${config.appTitle}`);
2016-04-17 15:38:06 +00:00
$('.plyr audio').attr('title', `${song.artist.name} - ${song.title}`);
2016-02-18 14:59:09 +00:00
2016-01-25 07:21:00 +00:00
// We'll just "restart" playing the song, which will handle notification, scrobbling etc.
this.restart();
},
/**
* Restart playing a song.
*/
restart() {
2016-03-28 13:38:14 +00:00
const song = queueStore.current;
2015-12-13 04:42:28 +00:00
2015-12-20 12:17:35 +00:00
// Record the UNIX timestamp the song start playing, for scrobbling purpose
song.playStartTime = Math.floor(Date.now() / 1000);
2016-06-25 05:24:55 +00:00
event.emit('song:played', song);
2015-12-13 04:42:28 +00:00
2016-01-25 07:21:00 +00:00
this.player.restart();
2015-12-13 04:42:28 +00:00
this.player.play();
2015-12-23 06:26:16 +00:00
// Register the play to the server
2015-12-13 04:42:28 +00:00
songStore.registerPlay(song);
// Show the notification if we're allowed to
2016-04-05 09:19:20 +00:00
if (!window.Notification || !preferences.notify) {
2015-12-13 04:42:28 +00:00
return;
}
2015-12-20 02:07:39 +00:00
try {
2016-03-28 13:38:14 +00:00
const notification = new Notification(`${song.title}`, {
2015-12-20 02:07:39 +00:00
icon: song.album.cover,
2016-04-17 15:38:06 +00:00
body: `${song.album.name} ${song.artist.name}`
2015-12-20 02:07:39 +00:00
});
notification.onclick = () => window.focus();
// Close the notif after 5 secs.
window.setTimeout(() => notification.close(), 5000);
} catch (e) {
// Notification fails.
2015-12-20 02:07:39 +00:00
// @link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
}
2015-12-13 04:42:28 +00:00
},
/**
2016-03-18 04:45:12 +00:00
* The next song in the queue.
2015-12-13 04:42:28 +00:00
* If we're in REPEAT_ALL mode and there's no next song, just get the first song.
2016-01-17 14:26:24 +00:00
*
* @return {Object} The song
2015-12-13 04:42:28 +00:00
*/
2016-03-18 04:45:12 +00:00
get next() {
2016-03-28 13:38:14 +00:00
const next = queueStore.next;
2015-12-13 04:42:28 +00:00
if (next) {
return next;
}
2016-04-05 09:19:20 +00:00
if (preferences.repeatMode === 'REPEAT_ALL') {
2016-03-18 04:45:12 +00:00
return queueStore.first;
2015-12-13 04:42:28 +00:00
}
},
/**
2016-03-18 04:45:12 +00:00
* The previous song in the queue.
2015-12-13 04:42:28 +00:00
* If we're in REPEAT_ALL mode and there's no prev song, get the last song.
2016-01-17 14:26:24 +00:00
*
* @return {Object} The song
2015-12-13 04:42:28 +00:00
*/
2016-03-18 04:45:12 +00:00
get previous() {
2016-03-28 13:38:14 +00:00
const prev = queueStore.previous;
2015-12-13 04:42:28 +00:00
if (prev) {
return prev;
}
2016-04-05 09:19:20 +00:00
if (preferences.repeatMode === 'REPEAT_ALL') {
2016-03-18 04:45:12 +00:00
return queueStore.last;
2015-12-13 04:42:28 +00:00
}
},
/**
* Circle through the repeat mode.
* The selected mode will be stored into local storage as well.
*/
changeRepeatMode() {
2016-04-05 09:19:20 +00:00
let idx = this.repeatModes.indexOf(preferences.repeatMode) + 1;
2016-03-28 13:38:14 +00:00
if (idx >= this.repeatModes.length) {
idx = 0;
2015-12-13 04:42:28 +00:00
}
2016-04-05 09:19:20 +00:00
preferences.repeatMode = this.repeatModes[idx];
2015-12-13 04:42:28 +00:00
},
/**
* Play the prev song in the queue, if one is found.
* If the prev song is not found and the current mode is NO_REPEAT, we stop completely.
*/
playPrev() {
2016-02-21 06:28:22 +00:00
// If the song's duration is greater than 5 seconds and we've passed 5 seconds into it,
// restart playing instead.
2016-03-18 04:45:12 +00:00
if (this.player.media.currentTime > 5 && queueStore.current.length > 5) {
this.player.restart();
return;
}
2016-03-28 13:38:14 +00:00
const prev = this.previous;
2015-12-13 04:42:28 +00:00
2016-04-05 09:19:20 +00:00
if (!prev && preferences.repeatMode === 'NO_REPEAT') {
2015-12-13 04:42:28 +00:00
this.stop();
return;
}
this.play(prev);
},
/**
* Play the next song in the queue, if one is found.
* If the next song is not found and the current mode is NO_REPEAT, we stop completely.
*/
playNext() {
2016-03-28 13:38:14 +00:00
const next = this.next;
2015-12-13 04:42:28 +00:00
2016-04-05 09:19:20 +00:00
if (!next && preferences.repeatMode === 'NO_REPEAT') {
2015-12-13 04:42:28 +00:00
// Nothing lasts forever, even cold November rain.
this.stop();
return;
}
this.play(next);
},
/**
* Set the volume level.
*
2016-01-17 14:26:24 +00:00
* @param {Number} volume 0-10
* @param {Boolean=true} persist Whether the volume should be saved into local storage
2015-12-13 04:42:28 +00:00
*/
setVolume(volume, persist = true) {
this.player.setVolume(volume);
if (persist) {
2016-04-05 09:19:20 +00:00
preferences.volume = volume;
2015-12-13 04:42:28 +00:00
}
this.$volumeInput.val(volume);
},
/**
* Mute playback.
*/
mute() {
this.setVolume(0, false);
},
/**
* Unmute playback.
*/
unmute() {
// If the saved volume is 0, we unmute to the default level (7).
2016-04-05 09:19:20 +00:00
if (preferences.volume === '0' || preferences.volume === 0) {
preferences.volume = 7;
2015-12-13 04:42:28 +00:00
}
2016-04-05 09:19:20 +00:00
this.setVolume(preferences.volume);
2015-12-13 04:42:28 +00:00
},
/**
* Completely stop playback.
*/
stop() {
$('title').text(config.appTitle);
this.player.pause();
this.player.seek(0);
2016-03-18 04:45:12 +00:00
if (queueStore.current) {
queueStore.current.playbackState = 'stopped';
}
2015-12-13 04:42:28 +00:00
},
/**
* Pause playback.
*/
pause() {
this.player.pause();
2016-03-18 04:45:12 +00:00
queueStore.current.playbackState = 'paused';
2015-12-13 04:42:28 +00:00
},
/**
* Resume playback.
*/
resume() {
this.player.play();
2016-03-18 04:45:12 +00:00
queueStore.current.playbackState = 'playing';
2016-06-25 05:24:55 +00:00
event.emit('song:played', queueStore.current);
2015-12-13 04:42:28 +00:00
},
/**
* Queue up songs (replace them into the queue) and start playing right away.
*
* @param {?Array.<Object>} songs An array of song objects. Defaults to all songs if null.
* @param {Boolean=false} shuffled Whether to shuffle the songs before playing.
2015-12-13 04:42:28 +00:00
*/
queueAndPlay(songs = null, shuffled = false) {
2015-12-13 04:42:28 +00:00
if (!songs) {
2016-03-18 04:45:12 +00:00
songs = songStore.all;
2015-12-13 04:42:28 +00:00
}
if (!songs.length) {
return;
}
if (shuffled) {
songs = shuffle(songs);
2015-12-13 04:42:28 +00:00
}
queueStore.queue(songs, true);
2016-06-25 05:24:55 +00:00
loadMainView('queue');
2015-12-13 04:42:28 +00:00
// Wrap this inside a nextTick() to wait for the DOM to complete updating
// and then play the first song in the queue.
2016-06-25 05:24:55 +00:00
Vue.nextTick(() => this.play(queueStore.first));
2015-12-13 04:42:28 +00:00
},
/**
* Play the first song in the queue.
* If the current queue is empty, try creating it by shuffling all songs.
*/
playFirstInQueue() {
2016-03-18 04:45:12 +00:00
if (!queueStore.all.length) {
2015-12-13 04:42:28 +00:00
this.queueAndPlay();
return;
}
2016-03-18 04:45:12 +00:00
this.play(queueStore.first);
2015-12-13 04:42:28 +00:00
},
/**
* Play all songs by an artist.
*
* @param {Object} artist The artist object
* @param {Boolean=true} shuffled Whether to shuffle the songs
*/
playAllByArtist(artist, shuffled = true) {
this.queueAndPlay(artist.songs, shuffled);
},
/**
* Play all songs in an album.
*
* @param {Object} album The album object
* @param {Boolean=true} shuffled Whether to shuffle the songs
*/
playAllInAlbum(album, shuffled = true) {
if (!shuffled) {
this.queueAndPlay(orderBy(album.songs, 'track'));
return;
}
this.queueAndPlay(album.songs, true);
},
2015-12-13 04:42:28 +00:00
};