import { HowlAudioSource } from "./HowlAudioSource"; export class HowlAudioController extends EventTarget { readonly source: HowlAudioSource; autoPlay: boolean; isPlay: boolean; volume: number; constructor(source: HowlAudioSource) { super(); this.source = source; this.autoPlay = false; this.isPlay = false; this.source.onEnded = () => { this._end(); }; this.source.onError = (e) => { if (typeof e === "string") { this._handleError(new Error(e)); } else { this._handleError(e instanceof Error ? e : e.error); } }; this.volume = 1; } _end() { this.isPlay = false; this.dispatchEvent(new Event("ended")); this.source.reset(); } _handleError(error: Error) { this.isPlay = false; this.dispatchEvent(new ErrorEvent("error", error)); } play() { // 必要なら入れる // if (this.isPlay) return; this.isPlay = true; /* if (isIOSorMacintoshTouchDevice()) { if (this.source.readyState !== "ready") { this.isPlay = false; this._handleError(new Error("not ready")); return; } } */ this.source.audio.volume(this.volume); this.source.audio.stop(); this.source.audio.play(); } changeVolume(volume: number) { this.volume = volume; if (this.source != null) { this.source.audio.volume(this.volume); } } changeDevice(id: string) { // unsupport /* const sinkable = this.source.audio as any as { setSinkId?: (id: string) => void; }; if (sinkable.setSinkId != null) { sinkable.setSinkId(id); } */ } destroy() { if (this.isPlay) { this.source.reset(); } } }