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.

This commit is contained in:
Mekan1206
2026-08-16 19:50:22 +05:00
parent c78f9bc944
commit 9248ef775a
2 changed files with 78 additions and 45 deletions

View File

@@ -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}`;

View File

@@ -41,7 +41,7 @@
</div>
</div>
<audio id="mm-chat-sound" src="{{ asset('assets/panel/audio/sound.mp3') }}" preload="auto" playsinline hidden></audio>
<audio id="mm-chat-sound" src="/assets/panel/audio/sound.mp3" preload="auto" playsinline></audio>
<script>
window.MM_CHAT = {
conversationsUrl: @json(url('/panel/chat/conversations')),