Refactor playSound function for better reusability

This commit is contained in:
Thorsten Sommer 2026-01-09 19:38:41 +01:00
parent f75c5a21b8
commit 8aad2a9ee4
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -27,28 +27,28 @@ window.scrollToBottom = function(element) {
element.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' }); element.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' });
} }
window.playSound = function(soundPath) {
try {
const audio = new Audio(soundPath);
audio.play().catch(error => {
console.warn('Failed to play sound effect:', error);
});
} catch (error) {
console.warn('Error creating audio element:', error);
}
};
let mediaRecorder; let mediaRecorder;
let actualRecordingMimeType; let actualRecordingMimeType;
let changedMimeType = false; let changedMimeType = false;
let pendingChunkUploads = 0; let pendingChunkUploads = 0;
window.audioRecorder = { window.audioRecorder = {
playSound: function(soundPath) {
try {
const audio = new Audio(soundPath);
audio.play().catch(error => {
console.warn('Failed to play sound effect:', error);
});
} catch (error) {
console.warn('Error creating audio element:', error);
}
},
start: async function (dotnetRef, desiredMimeTypes = []) { start: async function (dotnetRef, desiredMimeTypes = []) {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
// Play start recording sound effect: // Play start recording sound effect:
this.playSound('/sounds/start_recording.ogg'); window.playSound('/sounds/start_recording.ogg');
// When only one mime type is provided as a string, convert it to an array: // When only one mime type is provided as a string, convert it to an array:
if (typeof desiredMimeTypes === 'string') { if (typeof desiredMimeTypes === 'string') {
@ -138,7 +138,7 @@ window.audioRecorder = {
console.log('Audio recording - all chunks uploaded, finalizing.'); console.log('Audio recording - all chunks uploaded, finalizing.');
// Play stop recording sound effect: // Play stop recording sound effect:
window.audioRecorder.playSound('/sounds/stop_recording.ogg'); window.playSound('/sounds/stop_recording.ogg');
// Stop all tracks to release the microphone: // Stop all tracks to release the microphone:
mediaRecorder.stream.getTracks().forEach(track => track.stop()); mediaRecorder.stream.getTracks().forEach(track => track.stop());