commit 12/11/2025

This commit is contained in:
2025-11-12 10:15:03 +07:00
parent bf554b4f15
commit 46be98363a
11 changed files with 134 additions and 89 deletions

View File

@@ -251,94 +251,61 @@ function GetListeningZones() {
}
/**
* Starts live audio for the selected broadcast zone.
* @param {String} bz Broadcast Zone
* Open or Close Live Audio for the selected Broadcast Zone.
* @param {string} command either 'Open' or 'Close'
* @param {string} bz Broadcast Zone
* @param {Function} cbOK callback function on success
* @param {Function} cbFail callback function on failure
*/
function StartLiveAudio(bz, cbOK = null, cbFail = null) {
if (bz && bz.length > 0) {
let playurl = `/api/LiveAudio/Open/${bz}`;
const listenaudio = document.getElementById('listenaudio');
if (listenaudio) {
fetch(playurl, { method: 'GET' })
.then(response => {
console.log("Fetch response for Live Audio:", JSON.stringify(response));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.blob();
})
.then(blob => {
console.log(`Received audio stream for Broadcast Zone: ${bz}`);
const url = window.URL.createObjectURL(blob);
if (listenaudio) {
listenaudio.pause();
listenaudio.src = url;
listenaudio.load();
listenaudio.play();
console.log(`Started Live Audio for Broadcast Zone: ${bz}`);
listenaudio.setAttribute('visibility', 'visible');
if (cbOK) cbOK();
} else new Error("Listening audio element not found.");
})
.catch(error => {
alert(`Error starting Live Audio for Broadcast Zone: ${bz}. ${error}`);
if (cbFail) cbFail();
});
} else {
alert("Listening audio element not found.");
if (cbFail) cbFail();
}
} else {
alert("Please select a Broadcast Zone to start Live Audio.");
if (cbFail) cbFail();
function LiveAudioCommand(command, bz, cbOK = null, cbFail = null) {
function raise_cbOK(value=null){
if (cbOK) cbOK(value);
}
function raise_cbFail(value){
if (cbFail) cbFail(value);
}
if (command && command.length>0){
if (bz && bz.length>0){
if (command === 'Open' || command === 'Close'){
let url = `/api/LiveAudio`;
let payload = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
command: command,
broadcastzone: bz
})
};
fetch(url, payload)
.then(response => {
console.log(`Fetch response for Live Audio Command ${command}:`, JSON.stringify(response));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(`Live Audio Command ${command} for Broadcast Zone: ${bz} executed on server.`, data);
raise_cbOK(data);
})
.catch(error => {
console.log(`Error executing Live Audio Command ${command} for Broadcast Zone: ${bz} on server. ${error}`);
raise_cbFail(error);
});
} else raise_cbFail("LiveAudioCommand: Unknown command "+command);
} else raise_cbFail("LiveAudioCommand: Broadcast Zone is empty");
} else raise_cbFail("LiveAudioCommand: command is empty");
}
/**
* Stops live audio for the selected broadcast zone.
* @param {String} bz Broadcast Zone
* @param {Function} cbOK Callback function on success
* @param {Function} cbFail Callback function on failure
* Websocket for streaming
*/
function StopLiveAudio(bz, cbOK = null, cbFail = null) {
if (bz && bz.length > 0) {
const listenaudio = document.getElementById('listenaudio');
if (listenaudio) {
listenaudio.pause();
listenaudio.src = "";
console.log("Stopped Live Audio.");
let url = `/api/LiveAudio/Close/${bz}`;
fetch(url, { method: 'GET' })
.then(response => {
console.log("Fetch response for closing Live Audio:", JSON.stringify(response));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(`Live Audio for Broadcast Zone: ${bz} closed on server.`, data);
listenaudio.setAttribute('visibility', 'hidden');
if (cbOK) cbOK();
})
.catch(error => {
console.log(`Error closing Live Audio for Broadcast Zone: ${bz} on server. ${error}`);
if (cbFail) cbFail();
});
} else {
alert("Listening audio element not found.");
if (cbFail) cbFail();
}
} else {
alert("Please select a Broadcast Zone to stop Live Audio.");
if (cbFail) cbFail();
}
}
let streamws = null;
let mediasource = null;
$(document).ready(function () {
console.log("overview.js loaded");
@@ -350,12 +317,48 @@ $(document).ready(function () {
let $icon = $(this).find('svg');
if ($icon.hasClass('fa-stop')) {
console.log("Stopping Live Audio for Broadcast Zone:", bz);
StopLiveAudio(bz);
LiveAudioCommand('Close', bz, (okdata) =>{
$icon.toggleClass('fa-stop fa-play');
$("#listenzone").prop('disabled', false);
if (streamws) {
streamws.close();
streamws = null;
}
if (mediasource) {
mediasource.endOfStream();
mediasource = null;
}
let audio = document.getElementById('listenaudio');
audio.src = "";
}, (errdata) =>{
alert("Error stopping Live Audio: " + errdata);
});
} else {
console.log("Starting Live Audio for Broadcast Zone:", bz);
StartLiveAudio(bz);
LiveAudioCommand('Open', bz, (okdata) =>{
$icon.toggleClass('fa-stop fa-play');
$("#listenzone").prop('disabled', true);
streamws = new WebSocket(`ws://${window.location.host}/LiveAudio/ws`);
mediasource = new MediaSource();
let audio = document.getElementById('listenaudio');
audio.src = URL.createObjectURL(mediasource);
mediasource.addEventListener('sourceopen', () => {
const sourceBuffer = mediasource.addSourceBuffer('audio/mpeg; codecs="mp3"');
streamws.binaryType = 'arraybuffer';
streamws.onmessage = (event) => {
if (event.data instanceof ArrayBuffer) {
const chunk = new Uint8Array(event.data);
sourceBuffer.appendBuffer(chunk);
}
};
});
}, (errdata) =>{
alert("Error starting Live Audio: " + errdata);
});
}
$icon.toggleClass('fa-stop fa-play');
});
$('#clearpagingqueue').off('click').on('click', function () {
DoClear("QueuePaging/", "Paging Queue", (okdata) => {