Added check on audio interval

This commit is contained in:
Pax1601 2024-12-03 20:14:16 +01:00
parent f17ee42d63
commit c8da3e6da3
2 changed files with 34 additions and 28 deletions

View File

@ -8,7 +8,7 @@ export class FileSource extends AudioSource {
#source: AudioBufferSourceNode;
#duration: number = 0;
#currentPosition: number = 0;
#updateInterval: any;
#updateInterval: number | null;
#lastUpdateTime: number = 0;
#playing = false;
#audioBuffer: AudioBuffer;
@ -56,19 +56,21 @@ export class FileSource extends AudioSource {
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
this.#updateInterval = setInterval(() => {
/* Update the current position value every second */
const now = Date.now() / 1000;
this.#currentPosition += now - this.#lastUpdateTime;
this.#lastUpdateTime = now;
if (this.#updateInterval === null) {
this.#updateInterval = window.setInterval(() => {
/* Update the current position value every second */
const now = Date.now() / 1000;
this.#currentPosition += now - this.#lastUpdateTime;
this.#lastUpdateTime = now;
if (this.#currentPosition > this.#duration) {
this.#currentPosition = 0;
if (!this.#looping) this.pause();
}
if (this.#currentPosition > this.#duration) {
this.#currentPosition = 0;
if (!this.#looping) this.pause();
}
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
}, 1000);
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
}, 1000);
}
}
pause() {
@ -79,7 +81,8 @@ export class FileSource extends AudioSource {
const now = Date.now() / 1000;
this.#currentPosition += now - this.#lastUpdateTime;
clearInterval(this.#updateInterval);
if (this.#updateInterval) window.clearInterval(this.#updateInterval);
this.#updateInterval = null;
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
}

View File

@ -6,7 +6,7 @@ export class TextToSpeechSource extends AudioSource {
#source: AudioBufferSourceNode;
#duration: number = 0;
#currentPosition: number = 0;
#updateInterval: any;
#updateInterval: number | null;
#lastUpdateTime: number = 0;
#playing = false;
#audioBuffer: AudioBuffer;
@ -69,22 +69,24 @@ export class TextToSpeechSource extends AudioSource {
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
this.#updateInterval = setInterval(() => {
/* Update the current position value every second */
const now = Date.now() / 1000;
this.#currentPosition += now - this.#lastUpdateTime;
this.#lastUpdateTime = now;
if (this.#updateInterval === null) {
this.#updateInterval = window.setInterval(() => {
/* Update the current position value every second */
const now = Date.now() / 1000;
this.#currentPosition += now - this.#lastUpdateTime;
this.#lastUpdateTime = now;
if (this.#currentPosition > this.#duration) {
this.#currentPosition = 0;
if (!this.#looping) {
this.pause();
this.onMessageCompleted();
if (this.#currentPosition > this.#duration) {
this.#currentPosition = 0;
if (!this.#looping) {
this.pause();
this.onMessageCompleted();
}
}
}
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
}, 1000);
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
}, 1000);
}
}
pause() {
@ -95,7 +97,8 @@ export class TextToSpeechSource extends AudioSource {
const now = Date.now() / 1000;
this.#currentPosition += now - this.#lastUpdateTime;
clearInterval(this.#updateInterval);
if (this.#updateInterval) window.clearInterval(this.#updateInterval);
this.#updateInterval = null;
AudioSourcesChangedEvent.dispatch(getApp().getAudioManager().getSources());
}