From b940ca98e225dce6f3481a3013c49d4abce3e5c1 Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 28 Mar 2023 22:17:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20track=20state=20on=20finis?= =?UTF-8?q?hing=20track?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clients/discord/discord.voice.service.ts | 1 + src/clients/jellyfin/jellyfin.playstate.service.ts | 2 +- src/models/shared/Playlist.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/clients/discord/discord.voice.service.ts b/src/clients/discord/discord.voice.service.ts index 1e92493..ba5775f 100644 --- a/src/clients/discord/discord.voice.service.ts +++ b/src/clients/discord/discord.voice.service.ts @@ -266,6 +266,7 @@ export class DiscordVoiceService { const playlist = this.playbackService.getPlaylistOrDefault(); const finishedTrack = playlist.getActiveTrack(); + finishedTrack.playing = false; this.eventEmitter.emit('internal.audio.track.finish', finishedTrack); diff --git a/src/clients/jellyfin/jellyfin.playstate.service.ts b/src/clients/jellyfin/jellyfin.playstate.service.ts index c38221b..e394926 100644 --- a/src/clients/jellyfin/jellyfin.playstate.service.ts +++ b/src/clients/jellyfin/jellyfin.playstate.service.ts @@ -10,7 +10,7 @@ import { getSessionApi } from '@jellyfin/sdk/lib/utils/api/session-api'; import { Injectable, Logger } from '@nestjs/common'; import { OnEvent } from '@nestjs/event-emitter'; -import { Track } from 'src/models/shared/Track'; +import { Track } from '../../models/shared/Track'; import { PlaybackService } from '../../playback/playback.service'; diff --git a/src/models/shared/Playlist.ts b/src/models/shared/Playlist.ts index d86d0ae..76cfbab 100644 --- a/src/models/shared/Playlist.ts +++ b/src/models/shared/Playlist.ts @@ -92,12 +92,21 @@ export class Playlist { return 0; } + const previousTrackLength = this.tracks.length; + this.eventEmitter.emit('controls.playlist.tracks.enqueued', { count: tracks.length, activeTrack: this.activeTrackIndex, }); const length = this.tracks.push(...tracks); + // existing tracks are in the playlist, but none are playing. play the first track out of the new tracks + if (!this.hasAnyPlaying() && tracks.length > 0) { + this.activeTrackIndex = previousTrackLength; + this.announceTrackChange(); + return length; + } + // emit a track change if there is no item if (this.activeTrackIndex === undefined) { this.announceTrackChange(); @@ -128,6 +137,10 @@ export class Playlist { this.activeTrackIndex = undefined; } + private hasAnyPlaying() { + return this.tracks.some((track) => track.playing); + } + private announceTrackFinishIfSet() { if (this.activeTrackIndex === undefined) { return;