浏览代码

Merge pull request #12094 from Mugen87/dev4

Audio: Fix start time and offset
Mr.doob 7 年之前
父节点
当前提交
69c5a27e4a
共有 2 个文件被更改,包括 17 次插入8 次删除
  1. 6 3
      docs/api/audio/Audio.html
  2. 11 5
      src/audio/Audio.js

+ 6 - 3
docs/api/audio/Audio.html

@@ -35,8 +35,8 @@
 		//Load a sound and set it as the Audio object's buffer
 		audioLoader.load( 'sounds/ambient.ogg', function( buffer ) {
 			sound.setBuffer( buffer );
-			sound.setLoop(true);
-			sound.setVolume(0.5);
+			sound.setLoop( true );
+			sound.setVolume( 0.5 );
 			sound.play();
 		});
 		</code>
@@ -77,7 +77,10 @@
 		<div>Whether the audio is currently playing.</div>
 
 		<h3>[property:Number startTime]</h3>
-		<div>Point at which to start playback. Default is *0*.</div>
+		<div>The time at which the sound should begin to play. Same as the *when* paramter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *0*.</div>
+
+		<h3>[property:Number offset]</h3>
+		<div>An offset to the time within the audio buffer that playback should begin. Same as the *offset* paramter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *0*.</div>
 
 		<h3>[property:String source]</h3>
 		<div>An [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode AudioBufferSourceNode] created

+ 11 - 5
src/audio/Audio.js

@@ -21,6 +21,7 @@ function Audio( listener ) {
 	this.buffer = null;
 	this.loop = false;
 	this.startTime = 0;
+	this.offset = 0;
 	this.playbackRate = 1;
 	this.isPlaying = false;
 	this.hasPlaybackControl = true;
@@ -84,7 +85,8 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
 		source.loop = this.loop;
 		source.onended = this.onEnded.bind( this );
 		source.playbackRate.setValueAtTime( this.playbackRate, this.startTime );
-		source.start( 0, this.startTime );
+		this.startTime = this.context.currentTime;
+		source.start( this.startTime, this.offset );
 
 		this.isPlaying = true;
 
@@ -103,9 +105,13 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		}
 
-		this.source.stop();
-		this.startTime = this.context.currentTime;
-		this.isPlaying = false;
+		if ( this.isPlaying === true ) {
+
+			this.source.stop();
+			this.offset += ( this.context.currentTime - this.startTime ) * this.playbackRate;
+			this.isPlaying = false;
+
+		}
 
 		return this;
 
@@ -121,7 +127,7 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
 		}
 
 		this.source.stop();
-		this.startTime = 0;
+		this.offset = 0;
 		this.isPlaying = false;
 
 		return this;