Update AudioService.js

Change tab to space
This commit is contained in:
Chuui9739 2025-03-05 17:30:11 +08:00 committed by GitHub
parent f4970a92f4
commit d69a4c3b6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -258,56 +258,56 @@ export class AudioService {
} }
processNextOperation() { processNextOperation() {
if (this.sourceBuffer.updating || this.pendingOperations.length === 0) { if (this.sourceBuffer.updating || this.pendingOperations.length === 0) {
return; return;
} }
// Don't process if audio is in error state // Don't process if audio is in error state
if (this.audio.error) { if (this.audio.error) {
console.warn("Skipping operation due to audio error"); console.warn("Skipping operation due to audio error");
return; return;
} }
const operation = this.pendingOperations.shift(); const operation = this.pendingOperations.shift();
try { try {
this.sourceBuffer.appendBuffer(operation.chunk); this.sourceBuffer.appendBuffer(operation.chunk);
// Set up event listeners // Set up event listeners
const onUpdateEnd = () => { const onUpdateEnd = () => {
operation.resolve(); operation.resolve();
this.sourceBuffer.removeEventListener("updateend", onUpdateEnd); this.sourceBuffer.removeEventListener("updateend", onUpdateEnd);
this.sourceBuffer.removeEventListener( this.sourceBuffer.removeEventListener(
"updateerror", "updateerror",
onUpdateError onUpdateError
); );
// Process the next operation // Process the next operation
this.processNextOperation(); this.processNextOperation();
}; };
const onUpdateError = (event) => { const onUpdateError = (event) => {
operation.reject(event); operation.reject(event);
this.sourceBuffer.removeEventListener("updateend", onUpdateEnd); this.sourceBuffer.removeEventListener("updateend", onUpdateEnd);
this.sourceBuffer.removeEventListener( this.sourceBuffer.removeEventListener(
"updateerror", "updateerror",
onUpdateError onUpdateError
); );
// Decide whether to continue processing // Decide whether to continue processing
if (event.name !== "InvalidStateError") { if (event.name !== "InvalidStateError") {
this.processNextOperation(); this.processNextOperation();
} }
}; };
this.sourceBuffer.addEventListener("updateend", onUpdateEnd); this.sourceBuffer.addEventListener("updateend", onUpdateEnd);
this.sourceBuffer.addEventListener("updateerror", onUpdateError); this.sourceBuffer.addEventListener("updateerror", onUpdateError);
} catch (error) { } catch (error) {
operation.reject(error); operation.reject(error);
// Only continue processing if it's not a fatal error // Only continue processing if it's not a fatal error
if (error.name !== "InvalidStateError") { if (error.name !== "InvalidStateError") {
this.processNextOperation(); this.processNextOperation();
} }
} }
} }
play() { play() {
if (this.audio && this.audio.readyState >= 2 && !this.audio.error) { if (this.audio && this.audio.readyState >= 2 && !this.audio.error) {
@ -387,64 +387,64 @@ export class AudioService {
} }
cancel() { cancel() {
if (this.controller) { if (this.controller) {
this.controller.abort(); this.controller.abort();
this.controller = null; this.controller = null;
} }
if (this.audio) { if (this.audio) {
this.audio.pause(); this.audio.pause();
this.audio.src = ""; this.audio.src = "";
this.audio = null; this.audio = null;
} }
if (this.mediaSource && this.mediaSource.readyState === "open") { if (this.mediaSource && this.mediaSource.readyState === "open") {
try { try {
this.mediaSource.endOfStream(); this.mediaSource.endOfStream();
} catch (e) { } catch (e) {
// Ignore errors during cleanup // Ignore errors during cleanup
} }
} }
this.mediaSource = null; this.mediaSource = null;
if (this.sourceBuffer) { if (this.sourceBuffer) {
this.sourceBuffer.removeEventListener("updateend", () => {}); this.sourceBuffer.removeEventListener("updateend", () => {});
this.sourceBuffer.removeEventListener("updateerror", () => {}); this.sourceBuffer.removeEventListener("updateerror", () => {});
this.sourceBuffer = null; this.sourceBuffer = null;
} }
this.serverDownloadPath = null; this.serverDownloadPath = null;
this.pendingOperations = []; this.pendingOperations = [];
} }
cleanup() { cleanup() {
if (this.audio) { if (this.audio) {
this.eventListeners.forEach((listeners, event) => { this.eventListeners.forEach((listeners, event) => {
listeners.forEach((callback) => { listeners.forEach((callback) => {
this.audio.removeEventListener(event, callback); this.audio.removeEventListener(event, callback);
}); });
}); });
this.audio.pause(); this.audio.pause();
this.audio.src = ""; this.audio.src = "";
this.audio = null; this.audio = null;
} }
if (this.mediaSource && this.mediaSource.readyState === "open") { if (this.mediaSource && this.mediaSource.readyState === "open") {
try { try {
this.mediaSource.endOfStream(); this.mediaSource.endOfStream();
} catch (e) { } catch (e) {
// Ignore errors during cleanup // Ignore errors during cleanup
} }
} }
this.mediaSource = null; this.mediaSource = null;
if (this.sourceBuffer) { if (this.sourceBuffer) {
this.sourceBuffer.removeEventListener("updateend", () => {}); this.sourceBuffer.removeEventListener("updateend", () => {});
this.sourceBuffer.removeEventListener("updateerror", () => {}); this.sourceBuffer.removeEventListener("updateerror", () => {});
this.sourceBuffer = null; this.sourceBuffer = null;
} }
this.serverDownloadPath = null; this.serverDownloadPath = null;
this.pendingOperations = []; this.pendingOperations = [];
} }
getDownloadUrl() { getDownloadUrl() {