Skip to content

Commit

Permalink
Fixed seekbar randomly getting stuck
Browse files Browse the repository at this point in the history
Fixed closing with Alt + F4
Fixed invalid sinkIds always assumed as default device
  • Loading branch information
Tomás Antunes committed May 21, 2023
1 parent 45b14d5 commit 5e563cd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "mega-soundboard",
"version": "0.6.2",
"version": "0.6.3",
"description": "Mega Soundboard",
"main": "out/main/main.js",
"scripts": {
"build": "npm run copy && tsc --build && npm run bundle",
"start": "npm run build && electron .",
"startLast": "electron .",
"bundle": "npm run bundle-index && npm run bundle-preload",
"bundle-index": "browserify out/renderer/index.js -o out/renderer/bundle.js --no-bundle-external",
"bundle-index": "browserify out/renderer/index.js -o out/renderer/bundle.js --no-bundle-external --debug",
"bundle-preload": "browserify out/shared/preload.js -o out/shared/preload-bundle.js --no-bundle-external",
"copy": "copyfiles src/res/**/* out -u 1",
"dist": "npm run build && electron-builder -p never",
Expand Down
3 changes: 2 additions & 1 deletion src/main/eventSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MS from "./ms";
export default class EventSender {

static send<T extends keyof Events>(name: T, param?: EventsMap[T]): void {
MS.instance.windowManager.mainWindow.webContents.send(name, param);
if (!MS.instance.windowManager.mainWindow.isDestroyed())
MS.instance.windowManager.mainWindow.webContents.send(name, param);
}
}
12 changes: 10 additions & 2 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,19 @@ function loadDevicesPanel(devices: MediaDeviceInfo[], settings: Settings): void
}

function selectDevices(settings: Settings): void {
mainDeviceDropdown.selectIfFound(item =>
const foundMain = mainDeviceDropdown.selectIfFound(item =>
item instanceof DropdownDeviceItem && item.device === settings.mainDevice);

secondaryDeviceDropdown.selectIfFound(item =>
// If main device not found, load Default.
if (!foundMain) mainDeviceDropdown.selectIfFound(item =>
item instanceof DropdownDeviceItem && item.device == "default");

const foundSecondary = secondaryDeviceDropdown.selectIfFound(item =>
item instanceof DropdownDeviceItem && item.device === settings.secondaryDevice);

// If secondary device not found, load None.
if (!foundSecondary) secondaryDeviceDropdown.selectIfFound(item =>
item instanceof DropdownDeviceItem && item.device === "");
}

function setVolumes(settings: Settings): void {
Expand Down
28 changes: 24 additions & 4 deletions src/renderer/models/audioInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@ export default class AudioInstance {
devices: IDevice[],
volumeMult: number,
): Promise<AudioInstance> {
const sinkIdPromises: Promise<void>[] = [];
const audioElements: HTMLAudioElement[] = [];
const stopEvent = new Event<void>();
const pauseEvent = new Event<void>();
const playEvent = new Event<void>();
const timeUpdateEvent = new Event<void>();

let isFirst = true;
for (const device of devices) {
const audio = new Audio(sound.path);

try {
await audio.setSinkId(device.id);
} catch (error) {
if (isFirst) { // We must play to at least one device.
// If setSinkId fails, it is set to the default device.
console.log(`Invalid sinkId: '${device.id}'. Using default device.`);
} else {
console.log(`Ignoring invalid sinkId '${device.id}'.`);
continue;
}
}

audio.addEventListener("ended", () => {
audioElements.splice(audioElements.indexOf(audio), 1);
if (audioElements.length <= 0) {
Expand All @@ -38,16 +51,23 @@ export default class AudioInstance {
});

audio.volume = Math.pow((device.volume / 100) * (sound.volume / 100) * (volumeMult), 2);
const p = audio.setSinkId(device.id).catch(() => { console.error(`Error setting SinkId for ${device.id}.`); });
sinkIdPromises.push(p);
audioElements.push(audio);
isFirst = false;
}

// Wait for metadata to load only if it's not ready. (otherwise loadedmetadata would never fire)
if (audioElements[0].readyState <= 0) {
await new Promise<void>((resolve) => {
audioElements[0].addEventListener("loadedmetadata", () => {
resolve();
});
});
}

audioElements[0].addEventListener("pause", () => pauseEvent.raise());
audioElements[0].addEventListener("play", () => playEvent.raise());
audioElements[0].addEventListener("timeupdate", () => timeUpdateEvent.raise());

await Promise.all(sinkIdPromises);
return new AudioInstance(
sound, audioElements, volumeMult, stopEvent, pauseEvent, playEvent, timeUpdateEvent
);
Expand Down
16 changes: 16 additions & 0 deletions src/res/news.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<h3>Version 0.6.3</h3>
<ul>
<li>
<span class="tag fix">FIX</span>
<span>The seekbar would get stuck sometimes.</span>
</li>
<li>
<span class="tag fix">FIX</span>
<span>ALT + F4 would not close the app properly.</span>
</li>
<li>
<span class="tag fix">FIX</span>
<span>Unavailable/removed/unplugged audio devices would cause problems.</span>
</li>
</ul>

<h3>Version 0.6.2</h3>
<ul>
<li>
Expand Down

0 comments on commit 5e563cd

Please sign in to comment.