Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #166074. When play fails, the sound must be removed from playing sounds. #172630

Merged
merged 2 commits into from Jan 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 29 additions & 15 deletions src/vs/platform/audioCues/browser/audioCueService.ts
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { raceTimeout } from 'vs/base/common/async';
import { Disposable } from 'vs/base/common/lifecycle';
import { FileAccess } from 'vs/base/common/network';
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
Expand Down Expand Up @@ -61,32 +60,25 @@ export class AudioCueService extends Disposable implements IAudioCueService {
return Math.max(Math.min(volume, 100), 0);
}

private playingSounds = new Set<Sound>();
private readonly playingSounds = new Set<Sound>();

public async playSound(sound: Sound, allowManyInParallel = false): Promise<void> {
if (!allowManyInParallel && this.playingSounds.has(sound)) {
return;
}

this.playingSounds.add(sound);

const url = FileAccess.asBrowserUri(
`vs/platform/audioCues/browser/media/${sound.fileName}`
).toString();
const audio = new Audio(url);
audio.volume = this.getVolumeInPercent() / 100;
audio.addEventListener('ended', () => {
this.playingSounds.delete(sound);
});

try {
try {
// Don't play when loading takes more than 1s, due to loading, decoding or playing issues.
// Delayed sounds are very confusing.
await raceTimeout(audio.play(), 1000);
} catch (e) {
console.error('Error while playing sound', e);
}
await playAudio(url, this.getVolumeInPercent() / 100);
} catch (e) {
console.error('Error while playing sound', e);
} finally {
audio.remove();
this.playingSounds.delete(sound);
}
}

Expand Down Expand Up @@ -134,6 +126,28 @@ export class AudioCueService extends Disposable implements IAudioCueService {
}
}

/**
* Play the given audio url.
* @volume value between 0 and 1
*/
function playAudio(url: string, volume: number): Promise<void> {
return new Promise((resolve, reject) => {
const audio = new Audio(url);
audio.volume = volume;
audio.addEventListener('ended', () => {
resolve();
});
audio.addEventListener('error', (e) => {
// When the error event fires, ended might not be called
reject(e.error);
});
audio.play().catch(e => {
// When play fails, the error event is not fired.
reject(e);
});
});
}

function eventFromObservable(observable: IObservable<any>): Event<void> {
return (listener) => {
let count = 0;
Expand Down