# HG changeset patch # User Lucas Thompson # Date 1481118886 0 # Node ID ec38b85be3ac359bd0c90478641c30f736745098 # Parent 8619f25ff52a1ee7f7f70cfd34f54e6b8df790d2 Create publicly accessible streams for subscribing to play state change and seeking events. diff -r 8619f25ff52a -r ec38b85be3ac src/app/services/audio-player/audio-player.service.ts --- a/src/app/services/audio-player/audio-player.service.ts Tue Dec 06 14:19:03 2016 +0000 +++ b/src/app/services/audio-player/audio-player.service.ts Wed Dec 07 13:54:46 2016 +0000 @@ -1,10 +1,29 @@ import {Injectable, Inject} from '@angular/core'; +import {Subject} from "rxjs/Subject"; +import {Observable} from "rxjs"; @Injectable() export class AudioPlayerService { + private currentObjectUrl: string; + private playingStateChange: Subject; + playingStateChange$: Observable; + private seeked: Subject; + seeked$: Observable; + constructor(@Inject(HTMLAudioElement) private audioElement: HTMLAudioElement /* TODO probably shouldn't play audio this way */, @Inject('AudioContext') private audioContext: AudioContext) { + this.currentObjectUrl = ''; + this.playingStateChange = new Subject(); + this.playingStateChange$ = this.playingStateChange.asObservable(); + this.seeked = new Subject(); + this.seeked$ = this.seeked.asObservable(); + this.audioElement.addEventListener('ended', () => { + this.playingStateChange.next(this.isPlaying()); + }); + this.audioElement.addEventListener('seeked', () => { + this.seeked.next(this.audioElement.currentTime); + }); } getCurrentTime(): number { @@ -20,12 +39,16 @@ } loadAudioFromUrl(url: string): void { + if (this.currentObjectUrl) + URL.revokeObjectURL(this.currentObjectUrl); + this.currentObjectUrl = url; this.audioElement.pause(); this.audioElement.src = url; } togglePlaying(): void { this.isPlaying() ? this.audioElement.pause() : this.audioElement.play(); + this.playingStateChange.next(this.isPlaying()); } setVolume(value: number): void {