AudioLoader.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { AudioContext } from '../audio/AudioContext.js';
  2. import { FileLoader } from './FileLoader.js';
  3. import { DefaultLoadingManager } from './LoadingManager.js';
  4. /**
  5. * @author Reece Aaron Lecrivain / http://reecenotes.com/
  6. */
  7. function AudioLoader( manager ) {
  8. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  9. }
  10. Object.assign( AudioLoader.prototype, {
  11. load: function ( url, onLoad, onProgress, onError ) {
  12. var loader = new FileLoader( this.manager );
  13. loader.setResponseType( 'arraybuffer' );
  14. loader.setPath( this.path );
  15. loader.load( url, function ( buffer ) {
  16. // Create a copy of the buffer. The `decodeAudioData` method
  17. // detaches the buffer when complete, preventing reuse.
  18. var bufferCopy = buffer.slice( 0 );
  19. var context = AudioContext.getContext();
  20. context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
  21. onLoad( audioBuffer );
  22. } );
  23. }, onProgress, onError );
  24. },
  25. setPath: function ( value ) {
  26. this.path = value;
  27. return this;
  28. }
  29. } );
  30. export { AudioLoader };
粤ICP备19079148号