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:
1 parent
c78f9bc944
commit
9248ef775a
2 files changed
+78
-45
No files matched your search
@@ -19,22 +19,22 @@
|
|||||||
echo: null,
|
echo: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const notifySound =
|
const SOUND_URL = "/assets/panel/audio/sound.mp3";
|
||||||
document.getElementById("mm-chat-sound") || new Audio("/assets/panel/audio/sound.mp3");
|
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
|
||||||
notifySound.preload = "auto";
|
const audioCtx = AudioContextClass ? new AudioContextClass() : null;
|
||||||
notifySound.playsInline = true;
|
const fallbackAudio = document.getElementById("mm-chat-sound");
|
||||||
notifySound.controls = false;
|
if (fallbackAudio) {
|
||||||
notifySound.muted = false;
|
fallbackAudio.src = SOUND_URL;
|
||||||
notifySound.volume = 1;
|
fallbackAudio.preload = "auto";
|
||||||
notifySound.setAttribute("playsinline", "true");
|
fallbackAudio.playsInline = true;
|
||||||
if (!notifySound.isConnected) {
|
fallbackAudio.controls = false;
|
||||||
notifySound.style.cssText = "position:absolute;width:0;height:0;opacity:0;pointer-events:none";
|
fallbackAudio.volume = 1;
|
||||||
document.body.appendChild(notifySound);
|
fallbackAudio.style.cssText = "position:absolute;left:0;bottom:0;width:1px;height:1px;opacity:0.01";
|
||||||
}
|
}
|
||||||
|
|
||||||
const soundedIds = new Set();
|
const soundedIds = new Set();
|
||||||
let inboxReady = false;
|
let inboxReady = false;
|
||||||
let soundReady = false;
|
let soundBuffer = null;
|
||||||
let soundPending = false;
|
let soundPending = false;
|
||||||
|
|
||||||
function headers(json) {
|
function headers(json) {
|
||||||
@@ -164,48 +164,80 @@
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
function unlockSound() {
|
function loadSoundBuffer() {
|
||||||
if (soundReady && !soundPending) {
|
if (!audioCtx) {
|
||||||
return;
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
notifySound.muted = true;
|
return fetch(SOUND_URL, { credentials: "same-origin", cache: "force-cache" })
|
||||||
const play = notifySound.play();
|
.then((response) => {
|
||||||
if (!play || typeof play.then !== "function") {
|
if (!response.ok) {
|
||||||
notifySound.muted = false;
|
throw new Error("Sound file missing");
|
||||||
soundReady = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
play
|
|
||||||
.then(() => {
|
|
||||||
notifySound.pause();
|
|
||||||
notifySound.currentTime = 0;
|
|
||||||
notifySound.muted = false;
|
|
||||||
soundReady = true;
|
|
||||||
if (soundPending) {
|
|
||||||
soundPending = false;
|
|
||||||
playNotifySound();
|
|
||||||
}
|
}
|
||||||
|
return response.arrayBuffer();
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.then((bytes) => audioCtx.decodeAudioData(bytes.slice(0)))
|
||||||
notifySound.muted = false;
|
.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() {
|
function playNotifySound() {
|
||||||
if (!soundReady) {
|
if (audioCtx && audioCtx.state !== "running") {
|
||||||
soundPending = true;
|
soundPending = true;
|
||||||
|
audioCtx.resume().then(() => {
|
||||||
|
if (audioCtx.state === "running") {
|
||||||
|
soundPending = false;
|
||||||
|
playNotifySound();
|
||||||
|
}
|
||||||
|
}).catch(() => {});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
notifySound.pause();
|
if (audioCtx && soundBuffer) {
|
||||||
notifySound.currentTime = 0;
|
const source = audioCtx.createBufferSource();
|
||||||
} catch (error) {
|
source.buffer = soundBuffer;
|
||||||
// Ignore until metadata is ready.
|
source.connect(audioCtx.destination);
|
||||||
|
source.start(0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
notifySound.play().catch(() => {
|
|
||||||
soundReady = false;
|
if (fallbackAudio) {
|
||||||
soundPending = true;
|
try {
|
||||||
});
|
fallbackAudio.currentTime = 0;
|
||||||
|
} catch (error) {
|
||||||
|
// Ignore until metadata is ready.
|
||||||
|
}
|
||||||
|
fallbackAudio.play().catch(() => {
|
||||||
|
soundPending = true;
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
soundPending = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function playIncomingSound(message) {
|
function playIncomingSound(message) {
|
||||||
@@ -345,6 +377,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function openConversation(conversationId) {
|
async function openConversation(conversationId) {
|
||||||
|
unlockSound();
|
||||||
state.activeId = conversationId;
|
state.activeId = conversationId;
|
||||||
const conversation = state.conversations.find((item) => item.id === conversationId);
|
const conversation = state.conversations.find((item) => item.id === conversationId);
|
||||||
const name = (conversation && conversation.user && conversation.user.name) || `#${conversationId}`;
|
const name = (conversation && conversation.user && conversation.user.name) || `#${conversationId}`;
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</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>
|
<script>
|
||||||
window.MM_CHAT = {
|
window.MM_CHAT = {
|
||||||
conversationsUrl: @json(url('/panel/chat/conversations')),
|
conversationsUrl: @json(url('/panel/chat/conversations')),
|
||||||
|
|||||||
Reference in new issue
Block a user