AudioBuffer.js 884 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.AudioBuffer = function ( context ) {
  5. this.context = context;
  6. this.ready = false;
  7. this.readyCallbacks = [];
  8. };
  9. THREE.AudioBuffer.prototype.load = function ( file ) {
  10. var scope = this;
  11. var request = new XMLHttpRequest();
  12. request.open( 'GET', file, true );
  13. request.responseType = 'arraybuffer';
  14. request.onload = function ( e ) {
  15. scope.context.decodeAudioData( this.response, function ( buffer ) {
  16. scope.buffer = buffer;
  17. scope.ready = true;
  18. for ( var i = 0; i < scope.readyCallbacks.length; i ++ ) {
  19. scope.readyCallbacks[ i ]( scope.buffer );
  20. }
  21. scope.readyCallbacks = [];
  22. } );
  23. };
  24. request.send();
  25. return this;
  26. };
  27. THREE.AudioBuffer.prototype.onReady = function ( callback ) {
  28. if ( this.ready ) {
  29. callback( this.buffer );
  30. } else {
  31. this.readyCallbacks.push( callback );
  32. }
  33. };
粤ICP备19079148号