104 lines
3.0 KiB
JavaScript
104 lines
3.0 KiB
JavaScript
// -------------------------------------------------------------
|
|
// AUDIO MANAGER (Bypasses Browser Autoplay Restrictions)
|
|
// -------------------------------------------------------------
|
|
const SOUND_URL = "/assets/panel/audio/sound.mp3";
|
|
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
|
|
|
|
let audioCtx = null;
|
|
let soundBuffer = null;
|
|
let isUnlocked = false;
|
|
|
|
// Single HTML5 Audio instance fallback
|
|
const fallbackAudio = document.getElementById("mm-chat-sound") || new Audio(SOUND_URL);
|
|
fallbackAudio.src = SOUND_URL;
|
|
fallbackAudio.preload = "auto";
|
|
|
|
// Pre-load sound buffer for Web Audio API
|
|
function loadSoundBuffer() {
|
|
fetch(SOUND_URL, { credentials: "same-origin", cache: "force-cache" })
|
|
.then((res) => {
|
|
if (!res.ok) throw new Error("Audio HTTP error");
|
|
return res.arrayBuffer();
|
|
})
|
|
.then((bytes) => {
|
|
if (audioCtx) {
|
|
return audioCtx.decodeAudioData(bytes);
|
|
}
|
|
})
|
|
.then((buffer) => {
|
|
if (buffer) soundBuffer = buffer;
|
|
})
|
|
.catch(() => { });
|
|
}
|
|
|
|
// MUST be invoked directly from a user gesture listener (e.g. click/keydown)
|
|
function unlockAudio() {
|
|
if (isUnlocked) return;
|
|
|
|
// Instantiate AudioContext on user gesture
|
|
if (!audioCtx && AudioContextClass) {
|
|
audioCtx = new AudioContextClass();
|
|
loadSoundBuffer();
|
|
}
|
|
|
|
if (audioCtx && audioCtx.state === "suspended") {
|
|
audioCtx.resume();
|
|
}
|
|
|
|
// Play a silent 1ms audio snippet on the HTML5 element to unlock media playback
|
|
fallbackAudio.volume = 0;
|
|
const promise = fallbackAudio.play();
|
|
if (promise !== undefined) {
|
|
promise
|
|
.then(() => {
|
|
fallbackAudio.pause();
|
|
fallbackAudio.currentTime = 0;
|
|
fallbackAudio.volume = 1;
|
|
isUnlocked = true;
|
|
removeUnlockListeners();
|
|
})
|
|
.catch(() => {
|
|
// User gesture hasn't registered yet
|
|
});
|
|
}
|
|
}
|
|
|
|
function removeUnlockListeners() {
|
|
["click", "pointerdown", "keydown", "touchstart"].forEach((event) => {
|
|
document.removeEventListener(event, unlockAudio, true);
|
|
});
|
|
}
|
|
|
|
// Attach global passive listeners for user gestures
|
|
["click", "pointerdown", "keydown", "touchstart"].forEach((event) => {
|
|
document.addEventListener(event, unlockAudio, { capture: true, passive: true });
|
|
});
|
|
|
|
// Safe playback function triggered by WebSocket events
|
|
function playNotifySound() {
|
|
// Check if audio has been unlocked by user gesture
|
|
if (!isUnlocked) {
|
|
console.warn("[MM Chat Sound]: Notification blocked — user has not interacted with the page yet.");
|
|
return;
|
|
}
|
|
|
|
// 1. Try Web Audio API (Best latency)
|
|
if (audioCtx && audioCtx.state === "running" && soundBuffer) {
|
|
try {
|
|
const source = audioCtx.createBufferSource();
|
|
source.buffer = soundBuffer;
|
|
source.connect(audioCtx.destination);
|
|
source.start(0);
|
|
return;
|
|
} catch (e) { }
|
|
}
|
|
|
|
// 2. HTML5 Audio Fallback
|
|
try {
|
|
fallbackAudio.currentTime = 0;
|
|
fallbackAudio.volume = 1;
|
|
fallbackAudio.play().catch((err) => {
|
|
console.warn("[MM Chat Sound]: Playback blocked by browser policy:", err.message);
|
|
});
|
|
} catch (e) { }
|
|
} |