Skip to content

Commit

Permalink
fix: mute/unmute just mutes
Browse files Browse the repository at this point in the history
  • Loading branch information
XeroxDev committed Feb 13, 2024
1 parent 716c464 commit 64d546d
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/actions/mute.action.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {KeyUpEvent, SDOnActionEvent, WillAppearEvent, WillDisappearEvent,} from 'streamdeck-typescript';
import {KeyUpEvent, SDOnActionEvent, StateType, WillAppearEvent, WillDisappearEvent,} from 'streamdeck-typescript';
import {YTMD} from '../ytmd';
import {DefaultAction} from './default.action';
import {StateOutput} from "ytmdesktop-ts-companion";

export class MuteAction extends DefaultAction<MuteAction> {
private events: { context: string, method: (state: StateOutput) => void }[] = [];
private muted: boolean = false;
private initialized = false;
private volume = 0;
private lastVolume = 0;

constructor(private plugin: YTMD, actionName: string) {
super(plugin, actionName);
Expand All @@ -20,7 +22,19 @@ export class MuteAction extends DefaultAction<MuteAction> {

found = {
context: context,
method: (state: StateOutput) => this.muted = state.player.volume === 0
method: (state: StateOutput) => {
if (!this.initialized) {
this.initialized = true;
this.volume = state.player.volume;
this.lastVolume = this.volume;
}
this.volume = state.player.volume;
if (this.volume > 0) {
this.lastVolume = this.volume;
}

this.plugin.setState(this.volume > 0 ? StateType.ON : StateType.OFF, context);
}
};

this.events.push(found);
Expand All @@ -41,11 +55,21 @@ export class MuteAction extends DefaultAction<MuteAction> {

@SDOnActionEvent('keyUp')
onKeypressUp(event: KeyUpEvent) {
this.muted = !this.muted;
this.muted ? this.rest.mute().catch(reason => {
console.error(reason);
this.plugin.showAlert(event.context)
}) : this.rest.unmute().catch(reason => {
if (this.volume <= 0) {
this.volume = this.lastVolume;

this.rest.setVolume(this.volume).catch(reason => {
console.error(reason);
this.plugin.showAlert(event.context)
});

return;
}

this.lastVolume = this.volume;
this.volume = 0;

this.rest.setVolume(this.volume).catch(reason => {
console.error(reason);
this.plugin.showAlert(event.context)
});
Expand Down

0 comments on commit 64d546d

Please sign in to comment.