import { EventEmitter } from '@pixi/utils' ; import { Filter } from '../filters/Filter' ; import { IMedia } from '../interfaces/IMedia' ; import { LoadedCallback , Sound } from '../Sound' ; import { HTMLAudioContext } from './HTMLAudioContext' ; import { HTMLAudioInstance } from './HTMLAudioInstance' ; memberof extends PIXI . util . EventEmitter class HTMLAudioMedia extends EventEmitter implements IMedia { public parent : Sound ; private _source : HTMLAudioElement ; public init ( parent ) { this . parent = parent ; this . _source = parent . options . source as HTMLAudioElement || new Audio ( ) ; if ( parent . url ) { this . _source . src = parent . url ; } } public create ( ) { return new HTMLAudioInstance ( this ) ; } readonly public get isPlayable ( ) : boolean { return ! ! this . _source && this . _source . readyState === 4 ; } readonly public get duration ( ) : number { return this . _source . duration ; } readonly public get context ( ) : HTMLAudioContext { return this . parent . context as HTMLAudioContext ; } /** The collection of filters, does not apply to HTML Audio. */ public get filters ( ) : Filter [ ] { return null ; } public set filters ( _filters ) { console . warn ( 'HTML Audio does not support filters' ) ; } public destroy ( ) { this . removeAllListeners ( ) ; this . parent = null ; if ( this . _source ) { this . _source . src = '' ; this . _source . load ( ) ; this . _source = null ; } } type { HTMLAudioElement } readonly public get source ( ) : HTMLAudioElement { return this . _source ; } public load ( callback ? ) { const source = this . _source ; const sound = this . parent ; if ( source . readyState === 4 ) { sound . isLoaded = true ; const instance = sound . autoPlayStart ( ) ; if ( callback ) { setTimeout ( ( ) => { callback ( null , sound , instance ) ; } , 0 ) ; } return ; } if ( ! sound . url ) { callback ( new Error ( 'sound.url or sound.source must be set' ) ) ; return ; } source . src = sound . url ; const onLoad = ( ) => { removeListeners ( ) ; sound . isLoaded = true ; const instance = sound . autoPlayStart ( ) ; if ( callback ) { callback ( null , sound , instance ) ; } } ; const onAbort = ( ) => { removeListeners ( ) ; if ( callback ) { callback ( new Error ( 'Sound loading has been aborted' ) ) ; } } ; const onError = ( ) => { removeListeners ( ) ; const message = `Failed to load audio element (code: ${ source . error . code })` ; if ( callback ) { callback ( new Error ( message ) ) ; } else { console . error ( message ) ; } } ; const removeListeners = ( ) => { source . removeEventListener ( 'canplaythrough' , onLoad ) ; source . removeEventListener ( 'load' , onLoad ) ; source . removeEventListener ( 'abort' , onAbort ) ; source . removeEventListener ( 'error' , onError ) ; } ; source . addEventListener ( 'canplaythrough' , onLoad , false ) ; source . addEventListener ( 'load' , onLoad , false ) ; source . addEventListener ( 'abort' , onAbort , false ) ; source . addEventListener ( 'error' , onError , false ) ; source . load ( ) ; } } export { HTMLAudioMedia } ;