UNPKG

@inworld/web-core

Version:
32 lines (31 loc) 951 B
export class Player { static getInstance() { if (!this.instance) { this.instance = new Player(); } return Player.instance; } constructor() { this.mediaStream = new MediaStream(); this.audioElement = document.createElement('audio'); this.mediaStream = new MediaStream(); document.body.append(this.audioElement); } setStream(stream) { this.mediaStream = stream; this.audioElement.srcObject = stream; this.audioElement.autoplay = true; this.audioElement.play(); } setTrack(track) { if (!this.audioElement.srcObject) { this.audioElement.srcObject = this.mediaStream; } this.mediaStream.addTrack(track); this.audioElement.autoplay = true; } // workaround sound for browsers with restrictive play policies; playWorkaroundSound() { this.audioElement.play(); } }