import { Observable } from 'rxjs' ; import { flatMap , shareReplay , delay } from 'rxjs/operators' ; import { OWAudio } from './ow-audio' ; export class OWAudioFile { private id$ : Observable < string > ; constructor ( private readonly url ) { } public play ( ) { return this . ensure ( ) . pipe ( flatMap ( id => OWAudio . play ( id ) ) ) ; } public resume ( ) { return this . ensure ( ) . pipe ( flatMap ( id => OWAudio . resume ( id ) ) ) ; } public stop ( ) { return this . ensure ( ) . pipe ( flatMap ( id => OWAudio . stop ( id ) ) ) ; } public setVolume ( volume ) { return this . ensure ( ) . pipe ( flatMap ( ( ) => OWAudio . setVolume ( volume ) ) ) ; } private ensure ( ) { if ( ! this . id$ ) { this . id$ = OWAudio . create ( this . url ) . pipe ( shareReplay ( 1 ) ) ; } return this . id$ ; } }