mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-13 02:41:37 +00:00
Improved sound effect handling with shared audio context
This commit is contained in:
parent
9263e33bf2
commit
9cd502b51c
@ -27,14 +27,41 @@ window.scrollToBottom = function(element) {
|
||||
element.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' });
|
||||
}
|
||||
|
||||
window.playSound = function(soundPath) {
|
||||
// Shared audio context for sound effects (Web Audio API does not register with Media Session):
|
||||
let soundEffectContext = null;
|
||||
const soundEffectCache = new Map();
|
||||
|
||||
window.playSound = async function(soundPath) {
|
||||
try {
|
||||
const audio = new Audio(soundPath);
|
||||
audio.play().catch(error => {
|
||||
console.warn('Failed to play sound effect:', error);
|
||||
});
|
||||
// Create or reuse the audio context:
|
||||
if (!soundEffectContext || soundEffectContext.state === 'closed') {
|
||||
soundEffectContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||
}
|
||||
|
||||
// Resume if suspended (browser autoplay policy):
|
||||
if (soundEffectContext.state === 'suspended') {
|
||||
await soundEffectContext.resume();
|
||||
}
|
||||
|
||||
// Check the cache for already decoded audio:
|
||||
let audioBuffer = soundEffectCache.get(soundPath);
|
||||
|
||||
if (!audioBuffer) {
|
||||
// Fetch and decode the audio file:
|
||||
const response = await fetch(soundPath);
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
audioBuffer = await soundEffectContext.decodeAudioData(arrayBuffer);
|
||||
soundEffectCache.set(soundPath, audioBuffer);
|
||||
}
|
||||
|
||||
// Create a new source node and play:
|
||||
const source = soundEffectContext.createBufferSource();
|
||||
source.buffer = audioBuffer;
|
||||
source.connect(soundEffectContext.destination);
|
||||
source.start(0);
|
||||
|
||||
} catch (error) {
|
||||
console.warn('Error creating audio element:', error);
|
||||
console.warn('Failed to play sound effect:', error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -3,4 +3,5 @@
|
||||
- Improved error handling for model loading in provider dialogs (LLMs, embeddings, transcriptions).
|
||||
- Fixed a logging bug that prevented log events from being recorded in some cases.
|
||||
- Fixed a bug that allowed adding a provider without selecting a model.
|
||||
- Fixed a bug with local transcription providers by handling errors correctly when the local provider is unavailable.
|
||||
- Fixed a bug with local transcription providers by handling errors correctly when the local provider is unavailable.
|
||||
- Fixed a bug affecting the transcription preview: previously, when you stopped music or other media, recorded or dictated text, and then tried to resume playback, the media wouldn’t resume as expected. This behavior is now fixed.
|
||||
Loading…
Reference in New Issue
Block a user