提交 9ce14e39 编写于 作者: M Matthew Tung

Add functions for controlling audio (play/pause/stop)

上级 a2012918
......@@ -17,12 +17,15 @@ THREE.Audio = function ( listener ) {
this.panner = this.context.createPanner();
this.panner.connect( this.gain );
this.startTime = 0;
this.isPlaying = false;
};
THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
THREE.Audio.prototype.constructor = THREE.Audio;
THREE.Audio.prototype.load = function ( file ) {
THREE.Audio.prototype.load = function ( file, playImmediately ) {
var scope = this;
......@@ -34,8 +37,8 @@ THREE.Audio.prototype.load = function ( file ) {
scope.context.decodeAudioData( this.response, function ( 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 ) {
this.source.loop = value;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册