Files
kichikuou_xsystem35-sdl2/shell/cdda.ts
T
2017-04-15 12:39:19 +09:00

78 lines
2.6 KiB
TypeScript

/// <reference path="util.ts" />
/// <reference path="volume.ts" />
namespace xsystem35 {
export class CDPlayer {
private audio = <HTMLAudioElement>$('audio');
private blobs: Blob[];
private currentTrack: number;
waiting: boolean;
constructor(private imageLoader: ImageLoader, volumeControl: VolumeControl) {
this.blobs = [];
volumeControl.addEventListener(this.onVolumeChanged.bind(this));
this.audio.volume = volumeControl.volume();
this.audio.addEventListener('error', this.onAudioError.bind(this));
this.waiting = false;
this.removeUserGestureRestriction();
}
play(track: number, loop: number) {
this.currentTrack = track;
this.waiting = true;
if (this.blobs[track]) {
this.startPlayback(this.blobs[track], loop);
return;
}
this.imageLoader.getCDDA(track).then((blob) => {
this.blobs[track] = blob;
this.startPlayback(blob, loop);
});
}
stop() {
this.audio.pause();
this.currentTrack = null;
}
getPosition(): number {
if (!this.currentTrack)
return 0;
let time = Math.round(this.audio.currentTime * 75);
return this.currentTrack | time << 8;
}
private startPlayback(blob: Blob, loop: number) {
this.audio.setAttribute('src', URL.createObjectURL(blob));
this.audio.loop = (loop !== 0);
this.audio.load();
this.audio.play();
this.waiting = false;
}
private onVolumeChanged(evt: CustomEvent) {
this.audio.volume = evt.detail;
}
private onAudioError(err: ErrorEvent) {
let clone = document.importNode((<HTMLTemplateElement>$('#cdda-error')).content, true);
let toast = xsystem35.shell.addToast(clone, 'danger');
toast.querySelector('.cdda-reload-button').addEventListener('click', () => {
this.imageLoader.reloadImage().then(() => {
this.play(this.currentTrack, this.audio.loop ? 1 : 0);
(<HTMLElement>toast.querySelector('.btn-clear')).click();
});
});
}
private removeUserGestureRestriction() {
let hanlder = () => {
this.audio.load();
console.log('CDDA unlocked');
window.removeEventListener('touchend', hanlder);
};
window.addEventListener('touchend', hanlder);
}
}
}