Merge pull request #882 from daslyfe/audio_device_selection

bugfix: suspend and close exisiting audio context when changing interface
This commit is contained in:
Felix Roos 2024-01-01 14:22:10 +01:00 committed by GitHub
commit 5150ec06dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -30,11 +30,12 @@ let audioContext;
export const setDefaultAudioContext = () => {
audioContext = new AudioContext();
return audioContext;
};
export const getAudioContext = () => {
if (!audioContext) {
setDefaultAudioContext();
return setDefaultAudioContext();
}
return audioContext;
};

View File

@ -19,10 +19,14 @@ export const getAudioDevices = async () => {
};
export const setAudioDevice = async (id) => {
const audioCtx = getAudioContext();
let audioCtx = getAudioContext();
if (audioCtx.sinkId === id) {
return;
}
await audioCtx.suspend();
await audioCtx.close();
audioCtx = setDefaultAudioContext();
await audioCtx.resume();
const isValidID = (id ?? '').length > 0;
if (isValidID) {
try {
@ -30,9 +34,6 @@ export const setAudioDevice = async (id) => {
} catch {
logger('failed to set audio interface', 'warning');
}
} else {
// reset the audio context and dont set the sink id if it is invalid AKA System Standard selection
setDefaultAudioContext();
}
initializeAudioOutput();
};