mirror of
https://github.com/chuot/rdio-scanner.git
synced 2026-08-17 18:23:39 -06:00
Fix bug due to async decodeAudioData
This commit is contained in:
parent
b8acccfccc
commit
97399ba4c9
|
|
@ -69,10 +69,10 @@ export class AppRadioDisplayComponent implements OnDestroy, OnInit {
|
||||||
private handleRadioTimeEvent(event: RadioEvent): void {
|
private handleRadioTimeEvent(event: RadioEvent): void {
|
||||||
if ('time' in event) {
|
if ('time' in event) {
|
||||||
const currentFreq = this.currentCall.freqList
|
const currentFreq = this.currentCall.freqList
|
||||||
.reduce((cur: RadioCallFreq, freq: RadioCallFreq) => freq.pos < event.time ? freq : cur, null);
|
.reduce((cur: RadioCallFreq, freq: RadioCallFreq) => freq.pos <= event.time ? freq : cur, null);
|
||||||
|
|
||||||
const currentSrc = this.currentCall.srcList
|
const currentSrc = this.currentCall.srcList
|
||||||
.reduce((cur: RadioCallSrc, src: RadioCallSrc) => src.pos < event.time ? src : cur, null);
|
.reduce((cur: RadioCallSrc, src: RadioCallSrc) => src.pos <= event.time ? src : cur, null);
|
||||||
|
|
||||||
const currentError = currentFreq && currentFreq.errorCount;
|
const currentError = currentFreq && currentFreq.errorCount;
|
||||||
const currentFrequency = (currentFreq && currentFreq.freq);
|
const currentFrequency = (currentFreq && currentFreq.freq);
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,10 @@ export class AppRadioService implements OnDestroy {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
|
if (this.audioContext) {
|
||||||
|
this.audioContext.close();
|
||||||
|
}
|
||||||
|
|
||||||
this.event.complete();
|
this.event.complete();
|
||||||
|
|
||||||
this.unsubscribe();
|
this.unsubscribe();
|
||||||
|
|
@ -200,14 +204,17 @@ export class AppRadioService implements OnDestroy {
|
||||||
if (this.audioContext && !this.paused) {
|
if (this.audioContext && !this.paused) {
|
||||||
if (!call && !this.call.current && this.call.queue.length) {
|
if (!call && !this.call.current && this.call.queue.length) {
|
||||||
call = this.call.queue.shift();
|
call = this.call.queue.shift();
|
||||||
|
|
||||||
this.emitQueueEvent();
|
this.emitQueueEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (call && call.audio) {
|
if (call && call.audio) {
|
||||||
this.call.previous = this.call.current;
|
this.stop();
|
||||||
|
|
||||||
this.call.current = call;
|
this.call.current = call;
|
||||||
|
|
||||||
this.emitCallEvent();
|
this.emitCallEvent();
|
||||||
|
this.emitTimeEvent();
|
||||||
|
|
||||||
const audio = /^data:[^;]+;base64,/.test(call.audio) ? atob(call.audio.replace(/^[^,]*,/, '')) : call.audio;
|
const audio = /^data:[^;]+;base64,/.test(call.audio) ? atob(call.audio.replace(/^[^,]*,/, '')) : call.audio;
|
||||||
const arrayBuffer = new ArrayBuffer(audio.length);
|
const arrayBuffer = new ArrayBuffer(audio.length);
|
||||||
|
|
@ -217,11 +224,8 @@ export class AppRadioService implements OnDestroy {
|
||||||
arrayBufferView[i] = audio.charCodeAt(i);
|
arrayBufferView[i] = audio.charCodeAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.audioContext.decodeAudioData(arrayBuffer, (buffer) => {
|
this.audioContext.decodeAudioData(arrayBuffer).then((buffer) => {
|
||||||
if (this.audioSource) {
|
if (this.call.current) {
|
||||||
this.audioSource.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.audioSource = this.audioContext.createBufferSource();
|
this.audioSource = this.audioContext.createBufferSource();
|
||||||
|
|
||||||
this.audioSource.buffer = buffer;
|
this.audioSource.buffer = buffer;
|
||||||
|
|
@ -229,6 +233,8 @@ export class AppRadioService implements OnDestroy {
|
||||||
this.audioSource.onended = () => this.skip();
|
this.audioSource.onended = () => this.skip();
|
||||||
this.audioSource.start();
|
this.audioSource.start();
|
||||||
|
|
||||||
|
this.audioTimer = setInterval(() => this.emitTimeEvent(), 500);
|
||||||
|
|
||||||
if (this.audioContext.state === 'suspended') {
|
if (this.audioContext.state === 'suspended') {
|
||||||
const resume = () => {
|
const resume = () => {
|
||||||
this.audioContext.resume();
|
this.audioContext.resume();
|
||||||
|
|
@ -237,19 +243,13 @@ export class AppRadioService implements OnDestroy {
|
||||||
if (this.audioContext.state === 'running') {
|
if (this.audioContext.state === 'running') {
|
||||||
EVENTS.forEach((event) => document.body.removeEventListener(event, resume));
|
EVENTS.forEach((event) => document.body.removeEventListener(event, resume));
|
||||||
}
|
}
|
||||||
}, 0);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
EVENTS.forEach((event) => document.body.addEventListener(event, resume));
|
EVENTS.forEach((event) => document.body.addEventListener(event, resume));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
this.resetAudioTimer();
|
}).catch(() => this.skip(true));
|
||||||
|
|
||||||
}, (error: Error) => {
|
|
||||||
this.skip(true);
|
|
||||||
|
|
||||||
throw (error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -270,8 +270,6 @@ export class AppRadioService implements OnDestroy {
|
||||||
|
|
||||||
replay(): void {
|
replay(): void {
|
||||||
if (!this.paused) {
|
if (!this.paused) {
|
||||||
this.stopAudioTimer();
|
|
||||||
|
|
||||||
this.play(this.call.current || this.call.previous);
|
this.play(this.call.current || this.call.previous);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -296,22 +294,26 @@ export class AppRadioService implements OnDestroy {
|
||||||
}
|
}
|
||||||
|
|
||||||
stop(): void {
|
stop(): void {
|
||||||
this.stopAudioTimer();
|
if (this.call.current) {
|
||||||
|
if (this.audioTimer !== null) {
|
||||||
|
clearInterval(this.audioTimer);
|
||||||
|
this.audioTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.audioSource) {
|
if (this.audioSource) {
|
||||||
|
this.audioSource.onended = null;
|
||||||
|
this.audioSource.stop();
|
||||||
this.audioSource.disconnect();
|
this.audioSource.disconnect();
|
||||||
this.audioSource = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.audioStartTime = NaN;
|
this.audioStartTime = NaN;
|
||||||
|
|
||||||
if (this.call.current) {
|
|
||||||
this.call.previous = this.call.current;
|
this.call.previous = this.call.current;
|
||||||
this.call.current = null;
|
this.call.current = null;
|
||||||
}
|
|
||||||
|
|
||||||
this.emitCallEvent();
|
this.emitCallEvent();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
transform(call: RadioCall): RadioCall {
|
transform(call: RadioCall): RadioCall {
|
||||||
call.systemData = (call && this.systems
|
call.systemData = (call && this.systems
|
||||||
|
|
@ -332,7 +334,7 @@ export class AppRadioService implements OnDestroy {
|
||||||
this.audioContext = new AudioContext();
|
this.audioContext = new AudioContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.audioContext.state === 'suspended') {
|
if (this.audioContext && this.audioContext.state === 'suspended') {
|
||||||
this.audioContext.resume();
|
this.audioContext.resume();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -462,26 +464,6 @@ export class AppRadioService implements OnDestroy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private resetAudioTimer(): void {
|
|
||||||
this.stopAudioTimer();
|
|
||||||
this.startAudioTimer();
|
|
||||||
}
|
|
||||||
|
|
||||||
private startAudioTimer(): void {
|
|
||||||
if (this.audioTimer === null) {
|
|
||||||
this.emitTimeEvent();
|
|
||||||
|
|
||||||
this.audioTimer = setInterval(() => this.emitTimeEvent(), 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private stopAudioTimer(): void {
|
|
||||||
if (this.audioTimer !== null) {
|
|
||||||
clearInterval(this.audioTimer);
|
|
||||||
this.audioTimer = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private subscribeLiveFeed(): void {
|
private subscribeLiveFeed(): void {
|
||||||
if (!this.subscriptions.liveFeed.length) {
|
if (!this.subscriptions.liveFeed.length) {
|
||||||
const options = {
|
const options = {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue