mirror of
https://github.com/koel/koel
synced 2024-09-20 06:11:53 +00:00
chore: use "playable" as the terminology for Playback service
This commit is contained in:
parent
8761bb4a35
commit
181b2dce3a
1 changed files with 30 additions and 30 deletions
|
@ -17,7 +17,7 @@ import { audioService, http, socketService, volumeManager } from '@/services'
|
||||||
import { useEpisodeProgressTracking } from '@/composables'
|
import { useEpisodeProgressTracking } from '@/composables'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number of seconds before the current song ends to start preload the next one.
|
* The number of seconds before the current playable ends to start preload the next one.
|
||||||
*/
|
*/
|
||||||
const PRELOAD_BUFFER = 30
|
const PRELOAD_BUFFER = 30
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ class PlaybackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The next song in the queue.
|
* The next item in the queue.
|
||||||
* If we're in REPEAT_ALL mode and there's no next song, just get the first song.
|
* If we're in REPEAT_ALL mode and there's no next item, just get the first item.
|
||||||
*/
|
*/
|
||||||
public get next () {
|
public get next () {
|
||||||
if (queueStore.next) {
|
if (queueStore.next) {
|
||||||
|
@ -45,8 +45,8 @@ class PlaybackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The previous song in the queue.
|
* The previous item in the queue.
|
||||||
* If we're in REPEAT_ALL mode and there's no prev song, get the last song.
|
* If we're in REPEAT_ALL mode and there's no prev item, get the last item.
|
||||||
*/
|
*/
|
||||||
public get previous () {
|
public get previous () {
|
||||||
if (queueStore.previous) {
|
if (queueStore.previous) {
|
||||||
|
@ -100,7 +100,7 @@ class PlaybackService {
|
||||||
|
|
||||||
queueStore.queueIfNotQueued(playable)
|
queueStore.queueIfNotQueued(playable)
|
||||||
|
|
||||||
// If for any reason (most likely a bug), the requested song has been deleted, just attempt the next song.
|
// If for any reason (most likely a bug), the requested song has been deleted, just attempt the next item in queue.
|
||||||
if (isSong(playable) && playable.deleted) {
|
if (isSong(playable) && playable.deleted) {
|
||||||
logger.warn('Attempted to play a deleted song', playable)
|
logger.warn('Attempted to play a deleted song', playable)
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ class PlaybackService {
|
||||||
this.player.media.src = songStore.getSourceUrl(playable)
|
this.player.media.src = songStore.getSourceUrl(playable)
|
||||||
|
|
||||||
if (position === 0) {
|
if (position === 0) {
|
||||||
// We'll just "restart" playing the song, which will handle notification, scrobbling etc.
|
// We'll just "restart" playing the item, which will handle notification, scrobbling etc.
|
||||||
// Fixes #898
|
// Fixes #898
|
||||||
await this.restart()
|
await this.restart()
|
||||||
} else {
|
} else {
|
||||||
|
@ -177,7 +177,7 @@ class PlaybackService {
|
||||||
const playable = queueStore.current!
|
const playable = queueStore.current!
|
||||||
|
|
||||||
this.recordStartTime(playable)
|
this.recordStartTime(playable)
|
||||||
this.broadcastSong(playable)
|
this.broadcast(playable)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http.silently.put('queue/playback-status', {
|
http.silently.put('queue/playback-status', {
|
||||||
|
@ -211,11 +211,11 @@ class PlaybackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play the prev song in the queue, if one is found.
|
* Play the prev item the queue, if one is found.
|
||||||
* If the prev song is not found and the current mode is NO_REPEAT, we stop completely.
|
* If there's no prev item and the current mode is NO_REPEAT, we stop completely.
|
||||||
*/
|
*/
|
||||||
public async playPrev () {
|
public async playPrev () {
|
||||||
// If the song's duration is greater than 5 seconds, and we've passed 5 seconds into it,
|
// If the item's duration is greater than 5 seconds, and we've passed 5 seconds into it,
|
||||||
// restart playing instead.
|
// restart playing instead.
|
||||||
if (this.player.media.currentTime > 5 && queueStore.current!.length > 5) {
|
if (this.player.media.currentTime > 5 && queueStore.current!.length > 5) {
|
||||||
this.player.restart()
|
this.player.restart()
|
||||||
|
@ -231,8 +231,8 @@ class PlaybackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play the next song in the queue, if one is found.
|
* Play the next item in the queue, if one is found.
|
||||||
* If the next song is not found and the current mode is NO_REPEAT, we stop completely.
|
* If there's no next item and the current mode is NO_REPEAT, we stop completely.
|
||||||
*/
|
*/
|
||||||
public async playNext () {
|
public async playNext () {
|
||||||
if (!this.next && preferences.repeat_mode === 'NO_REPEAT') {
|
if (!this.next && preferences.repeat_mode === 'NO_REPEAT') {
|
||||||
|
@ -263,16 +263,16 @@ class PlaybackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async resume () {
|
public async resume () {
|
||||||
const song = queueStore.current!
|
const playable = queueStore.current!
|
||||||
|
|
||||||
if (!this.player.media.src) {
|
if (!this.player.media.src) {
|
||||||
// on first load when the queue is loaded from saved state, the player's src is empty
|
// on first load when the queue is loaded from saved state, the player's src is empty
|
||||||
// we need to properly set it as well as any kind of playback metadata
|
// we need to properly set it as well as any kind of playback metadata
|
||||||
this.player.media.src = songStore.getSourceUrl(song)
|
this.player.media.src = songStore.getSourceUrl(playable)
|
||||||
this.player.seek(commonStore.state.queue_state.playback_position);
|
this.player.seek(commonStore.state.queue_state.playback_position);
|
||||||
|
|
||||||
await this.setNowPlayingMeta(queueStore.current!)
|
await this.setNowPlayingMeta(queueStore.current!)
|
||||||
this.recordStartTime(song)
|
this.recordStartTime(playable)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -284,7 +284,7 @@ class PlaybackService {
|
||||||
queueStore.current!.playback_state = 'Playing'
|
queueStore.current!.playback_state = 'Playing'
|
||||||
navigator.mediaSession && (navigator.mediaSession.playbackState = 'playing')
|
navigator.mediaSession && (navigator.mediaSession.playbackState = 'playing')
|
||||||
|
|
||||||
this.broadcastSong(song)
|
this.broadcast(playable)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async toggle () {
|
public async toggle () {
|
||||||
|
@ -346,7 +346,7 @@ class PlaybackService {
|
||||||
song.play_count_registered = false
|
song.play_count_registered = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private broadcastSong (playable: Playable) {
|
private broadcast (playable: Playable) {
|
||||||
socketService.broadcast('SOCKET_SONG', playable)
|
socketService.broadcast('SOCKET_SONG', playable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -395,15 +395,15 @@ class PlaybackService {
|
||||||
})
|
})
|
||||||
|
|
||||||
let timeUpdateHandler = () => {
|
let timeUpdateHandler = () => {
|
||||||
const currentSong = queueStore.current
|
const currentPlayable = queueStore.current
|
||||||
|
|
||||||
if (!currentSong) return
|
if (!currentPlayable) return
|
||||||
|
|
||||||
if (!currentSong.play_count_registered && !this.isTranscoding) {
|
if (!currentPlayable.play_count_registered && !this.isTranscoding) {
|
||||||
// if we've passed 25% of the song, it's safe to say the song has been "played".
|
// if we've passed 25% of the playable, it's safe to say it has been "played".
|
||||||
// Refer to https://github.com/koel/koel/issues/1087
|
// Refer to https://github.com/koel/koel/issues/1087
|
||||||
if (!media.duration || media.currentTime * 4 >= media.duration) {
|
if (!media.duration || media.currentTime * 4 >= media.duration) {
|
||||||
this.registerPlay(currentSong)
|
this.registerPlay(currentPlayable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,27 +411,27 @@ class PlaybackService {
|
||||||
// every 5 seconds, we save the current playback position to the server
|
// every 5 seconds, we save the current playback position to the server
|
||||||
try {
|
try {
|
||||||
http.silently.put('queue/playback-status', {
|
http.silently.put('queue/playback-status', {
|
||||||
song: currentSong.id,
|
song: currentPlayable.id,
|
||||||
position: Math.ceil(media.currentTime)
|
position: Math.ceil(media.currentTime)
|
||||||
})
|
})
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the current song is an episode, we emit an event to update the progress on the client side as well
|
// if the current item is an episode, we emit an event to update the progress on the client side as well
|
||||||
if (isEpisode(currentSong)) {
|
if (isEpisode(currentPlayable)) {
|
||||||
eventBus.emit('EPISODE_PROGRESS_UPDATED', currentSong, Math.ceil(media.currentTime))
|
eventBus.emit('EPISODE_PROGRESS_UPDATED', currentPlayable, Math.ceil(media.currentTime))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextSong = queueStore.next
|
const nextPlayable = queueStore.next
|
||||||
|
|
||||||
if (!nextSong || nextSong.preloaded || this.isTranscoding) {
|
if (!nextPlayable || nextPlayable.preloaded || this.isTranscoding) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (media.duration && media.currentTime + PRELOAD_BUFFER > media.duration) {
|
if (media.duration && media.currentTime + PRELOAD_BUFFER > media.duration) {
|
||||||
this.preload(nextSong)
|
this.preload(nextPlayable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue