From 9248ef775a5de2bb2fdcbc77ae04f162ec8c30ab Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sun, 16 Aug 2026 19:50:22 +0500 Subject: [PATCH] Refactor audio handling in chat.js: implement AudioContext for improved sound playback, streamline sound loading, and enhance fallback audio element management. Update chat.blade.php to ensure proper audio element integration. --- public/assets/panel/js/chat.js | 121 ++++++++++++++++++---------- resources/views/nova/chat.blade.php | 2 +- 2 files changed, 78 insertions(+), 45 deletions(-) diff --git a/public/assets/panel/js/chat.js b/public/assets/panel/js/chat.js index bbe85e0..f45bcd0 100644 --- a/public/assets/panel/js/chat.js +++ b/public/assets/panel/js/chat.js @@ -19,22 +19,22 @@ echo: null, }; - const notifySound = - document.getElementById("mm-chat-sound") || new Audio("/assets/panel/audio/sound.mp3"); - notifySound.preload = "auto"; - notifySound.playsInline = true; - notifySound.controls = false; - notifySound.muted = false; - notifySound.volume = 1; - notifySound.setAttribute("playsinline", "true"); - if (!notifySound.isConnected) { - notifySound.style.cssText = "position:absolute;width:0;height:0;opacity:0;pointer-events:none"; - document.body.appendChild(notifySound); + const SOUND_URL = "/assets/panel/audio/sound.mp3"; + const AudioContextClass = window.AudioContext || window.webkitAudioContext; + const audioCtx = AudioContextClass ? new AudioContextClass() : null; + const fallbackAudio = document.getElementById("mm-chat-sound"); + if (fallbackAudio) { + fallbackAudio.src = SOUND_URL; + fallbackAudio.preload = "auto"; + fallbackAudio.playsInline = true; + fallbackAudio.controls = false; + fallbackAudio.volume = 1; + fallbackAudio.style.cssText = "position:absolute;left:0;bottom:0;width:1px;height:1px;opacity:0.01"; } const soundedIds = new Set(); let inboxReady = false; - let soundReady = false; + let soundBuffer = null; let soundPending = false; function headers(json) { @@ -164,48 +164,80 @@ return payload; } - function unlockSound() { - if (soundReady && !soundPending) { - return; + function loadSoundBuffer() { + if (!audioCtx) { + return Promise.resolve(); } - notifySound.muted = true; - const play = notifySound.play(); - if (!play || typeof play.then !== "function") { - notifySound.muted = false; - soundReady = true; - return; - } - play - .then(() => { - notifySound.pause(); - notifySound.currentTime = 0; - notifySound.muted = false; - soundReady = true; - if (soundPending) { - soundPending = false; - playNotifySound(); + return fetch(SOUND_URL, { credentials: "same-origin", cache: "force-cache" }) + .then((response) => { + if (!response.ok) { + throw new Error("Sound file missing"); } + return response.arrayBuffer(); }) - .catch(() => { - notifySound.muted = false; - }); + .then((bytes) => audioCtx.decodeAudioData(bytes.slice(0))) + .then((buffer) => { + soundBuffer = buffer; + }) + .catch(() => {}); + } + + loadSoundBuffer(); + + function unlockSound() { + if (audioCtx && audioCtx.state === "suspended") { + audioCtx.resume(); + } + if (fallbackAudio && fallbackAudio.paused) { + const play = fallbackAudio.play(); + if (play && typeof play.then === "function") { + play + .then(() => { + fallbackAudio.pause(); + fallbackAudio.currentTime = 0; + }) + .catch(() => {}); + } + } + if (soundPending) { + soundPending = false; + playNotifySound(); + } } function playNotifySound() { - if (!soundReady) { + if (audioCtx && audioCtx.state !== "running") { soundPending = true; + audioCtx.resume().then(() => { + if (audioCtx.state === "running") { + soundPending = false; + playNotifySound(); + } + }).catch(() => {}); return; } - try { - notifySound.pause(); - notifySound.currentTime = 0; - } catch (error) { - // Ignore until metadata is ready. + + if (audioCtx && soundBuffer) { + const source = audioCtx.createBufferSource(); + source.buffer = soundBuffer; + source.connect(audioCtx.destination); + source.start(0); + return; } - notifySound.play().catch(() => { - soundReady = false; - soundPending = true; - }); + + if (fallbackAudio) { + try { + fallbackAudio.currentTime = 0; + } catch (error) { + // Ignore until metadata is ready. + } + fallbackAudio.play().catch(() => { + soundPending = true; + }); + return; + } + + soundPending = true; } function playIncomingSound(message) { @@ -345,6 +377,7 @@ } async function openConversation(conversationId) { + unlockSound(); state.activeId = conversationId; const conversation = state.conversations.find((item) => item.id === conversationId); const name = (conversation && conversation.user && conversation.user.name) || `#${conversationId}`; diff --git a/resources/views/nova/chat.blade.php b/resources/views/nova/chat.blade.php index d2345a6..e226565 100644 --- a/resources/views/nova/chat.blade.php +++ b/resources/views/nova/chat.blade.php @@ -41,7 +41,7 @@ - +