AudioLoader.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { AudioContext } from '../audio/AudioContext.js';
  2. import { FileLoader } from './FileLoader.js';
  3. import { Loader } from './Loader.js';
  4. class AudioLoader extends Loader {
  5. constructor( manager ) {
  6. super( manager );
  7. }
  8. load( url, onLoad, onProgress, onError ) {
  9. const scope = this;
  10. const loader = new FileLoader( this.manager );
  11. loader.setResponseType( 'arraybuffer' );
  12. loader.setPath( this.path );
  13. loader.setRequestHeader( this.requestHeader );
  14. loader.setWithCredentials( this.withCredentials );
  15. loader.load( url, function ( buffer ) {
  16. try {
  17. // Create a copy of the buffer. The `decodeAudioData` method
  18. // detaches the buffer when complete, preventing reuse.
  19. const bufferCopy = buffer.slice( 0 );
  20. const context = AudioContext.getContext();
  21. context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
  22. onLoad( audioBuffer );
  23. } );
  24. } catch ( e ) {
  25. if ( onError ) {
  26. onError( e );
  27. } else {
  28. console.error( e );
  29. }
  30. scope.manager.itemError( url );
  31. }
  32. }, onProgress, onError );
  33. }
  34. }
  35. export { AudioLoader };
粤ICP备19079148号