Skip to content

Commit 4156fce

Browse files
authored
Merge pull request #17764 from mrdoob/audio
Audio: Refactored pause time code.
2 parents bf99d3c + 2889f7c commit 4156fce

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

docs/api/en/audio/Audio.html

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,11 @@ <h3>[property:AudioListener listener]</h3>
8585
<h3>[property:Number playbackRate]</h3>
8686
<p>Speed of playback. Default is *1*.</p>
8787

88-
<h3>[property:Number startTime]</h3>
89-
<p>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*.</p>
90-
9188
<h3>[property:Number offset]</h3>
92-
<p>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*.</p>
89+
<p>An offset to the time within the audio buffer that playback should begin. Same as the *offset* parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *0*.</p>
9390

9491
<h3>[property:Number duration]</h3>
95-
<p>Overrides the duration of the audio. Same as the *duration* paramter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *undefined* to play the whole buffer.</p>
92+
<p>Overrides the duration of the audio. Same as the *duration* parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *undefined* to play the whole buffer.</p>
9693

9794
<h3>[property:String source]</h3>
9895
<p>An [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode AudioBufferSourceNode] created
@@ -150,7 +147,7 @@ <h3>[method:Float getVolume]( value )</h3>
150147
Return the current volume.
151148
</p>
152149

153-
<h3>[method:Audio play]()</h3>
150+
<h3>[method:Audio play]( delay )</h3>
154151
<p>
155152
If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts playback.
156153
</p>
@@ -162,7 +159,7 @@ <h3>[method:Audio pause]()</h3>
162159

163160
<h3>[method:null onEnded]()</h3>
164161
<p>
165-
Called automatically when playback finished. Sets If [page:Audio.isPlaying isPlaying] to false.
162+
Called automatically when playback finished.
166163
</p>
167164

168165
<h3>[method:Audio setBuffer]( audioBuffer )</h3>
@@ -229,8 +226,7 @@ <h3>[method:Audio setVolume]( [param:Float value] )</h3>
229226

230227
<h3>[method:Audio stop]()</h3>
231228
<p>
232-
If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback,
233-
resets [page:Audio.startTime startTime] to *0* and sets [page:Audio.isPlaying isPlaying] to false.
229+
If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback.
234230
</p>
235231

236232
<h2>Source</h2>

docs/api/zh/audio/Audio.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ <h3>[property:Boolean isPlaying]</h3>
8484
<h3>[property:AudioListener listener]</h3>
8585
<p>A reference to the listener object of this audio.</p>
8686

87-
<h3>[property:Number startTime]</h3>
88-
<p>开始播放的时间. 和[link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start]()的*when*参数一样. 默认为 *0*.</p>
89-
9087
<h3>[property:Number offset]</h3>
9188
<p>音频开始播放的偏移时间. 和[link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start]()的*offset*参数一样. 默认为 *0*.</p>
9289

@@ -146,7 +143,7 @@ <h3>[method:Float getVolume]( value )</h3>
146143
返回音量.
147144
</p>
148145

149-
<h3>[method:Audio play]()</h3>
146+
<h3>[method:Audio play]( delay )</h3>
150147
<p>
151148
如果[page:Audio.hasPlaybackControl hasPlaybackControl]是true, 开始播放.
152149
</p>
@@ -158,7 +155,7 @@ <h3>[method:Audio pause]()</h3>
158155

159156
<h3>[method:null onEnded]()</h3>
160157
<p>
161-
播放完成后自动调用. 如果[page:Audio.isPlaying isPlaying]设置为false.
158+
播放完成后自动调用.
162159
</p>
163160

164161
<h3>[method:Audio setBuffer]( audioBuffer )</h3>
@@ -220,8 +217,7 @@ <h3>[method:Audio setVolume]( [param:Float value] )</h3>
220217

221218
<h3>[method:Audio stop]()</h3>
222219
<p>
223-
如果[page:Audio.hasPlaybackControl hasPlaybackControl]是true, 停止播放,
224-
重新设置[page:Audio.startTime startTime]为 *0* 和 设置 [page:Audio.isPlaying isPlaying]为false.
220+
如果[page:Audio.hasPlaybackControl hasPlaybackControl]是true, 停止播放.
225221
</p>
226222

227223
<h2>源码</h2>

src/Three.Legacy.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,19 +1918,32 @@ Object.defineProperties( WebVRManager.prototype, {
19181918

19191919
//
19201920

1921-
Audio.prototype.load = function ( file ) {
1921+
Object.defineProperties( Audio.prototype, {
19221922

1923-
console.warn( 'THREE.Audio: .load has been deprecated. Use THREE.AudioLoader instead.' );
1924-
var scope = this;
1925-
var audioLoader = new AudioLoader();
1926-
audioLoader.load( file, function ( buffer ) {
1923+
load: {
1924+
value: function ( file ) {
19271925

1928-
scope.setBuffer( buffer );
1926+
console.warn( 'THREE.Audio: .load has been deprecated. Use THREE.AudioLoader instead.' );
1927+
var scope = this;
1928+
var audioLoader = new AudioLoader();
1929+
audioLoader.load( file, function ( buffer ) {
19291930

1930-
} );
1931-
return this;
1931+
scope.setBuffer( buffer );
19321932

1933-
};
1933+
} );
1934+
return this;
1935+
1936+
}
1937+
},
1938+
startTime: {
1939+
set: function () {
1940+
1941+
console.warn( 'THREE.Audio: .startTime is now .play( delay ).' );
1942+
1943+
}
1944+
}
1945+
1946+
} );
19341947

19351948
AudioAnalyser.prototype.getData = function () {
19361949

src/audio/Audio.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class Audio extends Object3D {
1515
buffer: null | Audio;
1616
detune: number;
1717
loop: boolean;
18-
startTime: number;
1918
offset: number;
2019
duration: number | undefined;
2120
playbackRate: number;
@@ -30,7 +29,7 @@ export class Audio extends Object3D {
3029
setMediaElementSource( mediaElement: HTMLMediaElement ): this;
3130
setMediaStreamSource( mediaStream: MediaStream ): this;
3231
setBuffer( audioBuffer: AudioBuffer ): this;
33-
play(): this;
32+
play( delay?: number ): this;
3433
onEnded(): void;
3534
pause(): this;
3635
stop(): this;

src/audio/Audio.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ function Audio( listener ) {
2424
this.loop = false;
2525
this.loopStart = 0;
2626
this.loopEnd = 0;
27-
this.startTime = 0;
2827
this.offset = 0;
2928
this.duration = undefined;
3029
this.playbackRate = 1;
3130
this.isPlaying = false;
3231
this.hasPlaybackControl = true;
3332
this.sourceType = 'empty';
3433

34+
this._startedAt = 0;
35+
this._pausedAt = 0;
36+
3537
this.filters = [];
3638

3739
}
@@ -90,7 +92,9 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
9092

9193
},
9294

93-
play: function () {
95+
play: function ( delay ) {
96+
97+
if ( delay === undefined ) delay = 0;
9498

9599
if ( this.isPlaying === true ) {
96100

@@ -106,15 +110,15 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
106110

107111
}
108112

109-
var source = this.context.createBufferSource();
113+
this._startedAt = this.context.currentTime + delay;
110114

115+
var source = this.context.createBufferSource();
111116
source.buffer = this.buffer;
112117
source.loop = this.loop;
113118
source.loopStart = this.loopStart;
114119
source.loopEnd = this.loopEnd;
115120
source.onended = this.onEnded.bind( this );
116-
this.startTime = this.context.currentTime;
117-
source.start( this.startTime, this.offset, this.duration );
121+
source.start( this._startedAt, this._pausedAt + this.offset, this.duration );
118122

119123
this.isPlaying = true;
120124

@@ -138,9 +142,11 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
138142

139143
if ( this.isPlaying === true ) {
140144

145+
this._pausedAt = ( this.context.currentTime - this._startedAt ) * this.playbackRate;
146+
141147
this.source.stop();
142148
this.source.onended = null;
143-
this.offset += ( this.context.currentTime - this.startTime ) * this.playbackRate;
149+
144150
this.isPlaying = false;
145151

146152
}
@@ -158,9 +164,10 @@ Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
158164

159165
}
160166

167+
this._pausedAt = 0;
168+
161169
this.source.stop();
162170
this.source.onended = null;
163-
this.offset = 0;
164171
this.isPlaying = false;
165172

166173
return this;

0 commit comments

Comments
 (0)