import { Timer } from "../../timer/Timer" ; import { LoaderResource } from "pixi.js" ; import { Log } from "../../log/Log" ; import { AudioManager } from "../AudioManager" ; import { AudioType } from "../AudioType" ; export class AudioLoader { public static suffix : string [ ] = [ "mp3" , "ogg" , "wav" , "aac" ] ; private static loadAudio : Array < { next : ( ) => void ; resource : LoaderResource ; } > = new Array ( ) ; private static threadCount = 3 ; private static idle : number = 0 ; public static use ( resource , next ) { if ( resource . extension && AudioLoader . suffix . includes ( resource . extension ) ) { resource . xhr = null ; AudioLoader . loadAudio . push ( { resource , next , } ) ; for ( let i = AudioLoader . idle ; i < AudioLoader . threadCount ; i ++ ) { AudioLoader . pop ( ) ; } } else { next ( ) ; } } private static pop ( ) { if ( AudioLoader . idle > AudioLoader . threadCount ) { return ; } let audioObj = AudioLoader . loadAudio . pop ( ) ; if ( ! audioObj ) { return ; } AudioLoader . idle ++ ; let next = audioObj . next ; let resource = audioObj . resource ; AudioLoader . loadData ( resource , ( ) => { AudioLoader . idle -- ; for ( let i = AudioLoader . idle ; i < AudioLoader . threadCount ; i ++ ) { AudioLoader . pop ( ) ; } Timer . setTimeout ( ( ) => { next ( ) ; } , 10 ) ; } ) ; } private static loadData ( resource , next ) { resource . xhr = null ; if ( AudioManager . useAudioType == AudioType . AUDIO_CONTEXT ) { resource . data = null ; let xhr ; try { xhr = new XMLHttpRequest ( ) ; } catch ( e ) { try { xhr = new ( < any > window ) . ActiveXObject ( "Microsoft.XMLHttp" ) ; } catch ( e ) { } } if ( ! xhr ) { resource . error = new Error ( "资源加载出错！！！网络资源异常！" + resource . url + "\n" ) ; next ( ) ; return ; } xhr . open ( "GET" , resource . url , true ) ; xhr . responseType = "arraybuffer" ; xhr . onload = function ( ) { if ( xhr . readyState === 4 ) { if ( xhr . status == 200 || xhr . status == 304 ) { resource . data = xhr . response ; xhr = undefined ; AudioManager . loadAudio ( resource , next ) ; } else { resource . error = new Error ( "code:" + xhr . status + ":资源加载出错！！！网络资源异常！" + resource . url + "\n" ) ; next ( ) ; } } } ; xhr . onerror = function ( ) { resource . error = new Error ( "资源加载出错！！！网络资源异常！" + resource . url + "\n" ) ; next ( ) ; } ; xhr . send ( ) ; } else if ( AudioManager . useAudioType == AudioType . HTML ) { AudioManager . loadAudio ( resource , next ) ; } else { resource . error = new Error ( "资源加载出错！！！网络资源异常！" + resource . url + "\n" ) ; next ( ) ; } } }