|
@@ -17,12 +17,15 @@ THREE.Audio = function ( listener ) {
|
|
this.panner = this.context.createPanner();
|
|
this.panner = this.context.createPanner();
|
|
this.panner.connect( this.gain );
|
|
this.panner.connect( this.gain );
|
|
|
|
|
|
|
|
+ this.startTime = 0;
|
|
|
|
+ this.isPlaying = false;
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
|
|
THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
|
|
THREE.Audio.prototype.constructor = THREE.Audio;
|
|
THREE.Audio.prototype.constructor = THREE.Audio;
|
|
|
|
|
|
-THREE.Audio.prototype.load = function ( file ) {
|
|
|
|
|
|
+THREE.Audio.prototype.load = function ( file, playImmediately ) {
|
|
|
|
|
|
var scope = this;
|
|
var scope = this;
|
|
|
|
|
|
@@ -34,8 +37,8 @@ THREE.Audio.prototype.load = function ( file ) {
|
|
scope.context.decodeAudioData( this.response, function ( buffer ) {
|
|
scope.context.decodeAudioData( this.response, function ( buffer ) {
|
|
|
|
|
|
scope.source.buffer = buffer;
|
|
scope.source.buffer = buffer;
|
|
- scope.source.connect( scope.panner );
|
|
|
|
- scope.source.start( 0 );
|
|
|
|
|
|
+
|
|
|
|
+ if( playImmediately !== false ) scope.play();
|
|
|
|
|
|
} );
|
|
} );
|
|
|
|
|
|
@@ -46,6 +49,47 @@ THREE.Audio.prototype.load = function ( file ) {
|
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+THREE.Audio.prototype.play = function () {
|
|
|
|
+
|
|
|
|
+ if ( ! this.isPlaying ) {
|
|
|
|
+
|
|
|
|
+ var source = this.context.createBufferSource();
|
|
|
|
+
|
|
|
|
+ source.buffer = this.source.buffer;
|
|
|
|
+ source.loop = this.source.loop;
|
|
|
|
+ source.connect( this.panner );
|
|
|
|
+ source.start( 0, this.startTime );
|
|
|
|
+
|
|
|
|
+ this.isPlaying = true;
|
|
|
|
+
|
|
|
|
+ this.source = source;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else {
|
|
|
|
+
|
|
|
|
+ console.warn("Audio is already playing.")
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+THREE.Audio.prototype.pause = function () {
|
|
|
|
+
|
|
|
|
+ this.source.stop();
|
|
|
|
+ this.startTime = this.context.currentTime;
|
|
|
|
+ this.isPlaying = false;
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+THREE.Audio.prototype.stop = function () {
|
|
|
|
+
|
|
|
|
+ this.source.stop();
|
|
|
|
+ this.startTime = 0;
|
|
|
|
+ this.isPlaying = false;
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+
|
|
THREE.Audio.prototype.setLoop = function ( value ) {
|
|
THREE.Audio.prototype.setLoop = function ( value ) {
|
|
|
|
|
|
this.source.loop = value;
|
|
this.source.loop = value;
|