@@ -27,9 +27,9 @@ export class Player extends Destructable {
2727 * Get current playback speed
2828 */
2929 get rate ( ) {
30- if ( this . audio ) {
31- if ( this . audio . speed !== this . _rate ) {
32- this . audio . speed = this . _rate ; // restore the correct rate
30+ if ( this . audio ?. source ?. playbackRate . value ) {
31+ if ( this . audio . source . playbackRate . value !== this . _rate ) {
32+ this . audio . source . playbackRate . value = this . _rate ; // restore the correct rate
3333 }
3434 }
3535
@@ -44,8 +44,8 @@ export class Player extends Destructable {
4444
4545 this . _rate = value ;
4646
47- if ( this . audio ) {
48- this . audio . speed = value ;
47+ if ( this . audio ?. source ) {
48+ this . audio . source . playbackRate . value = value ;
4949
5050 if ( rateChanged ) {
5151 this . wf . invoke ( 'rateChanged' , [ value ] ) ;
@@ -54,28 +54,22 @@ export class Player extends Destructable {
5454 }
5555
5656 get duration ( ) {
57- return this . audio ?. duration ?? 0 ;
57+ return this . audio ?. buffer ?. duration ?? 0 ;
5858 }
5959
6060 get volume ( ) {
61- return this . audio ?. volume ?? 1 ;
61+ return this . audio ?. gain ?. gain . value ?? 1 ;
6262 }
6363
6464 set volume ( value : number ) {
6565 if ( this . audio ) {
6666
6767 const volumeChanged = this . volume !== value ;
6868
69+ this . audio . volume = value ;
70+
6971 if ( volumeChanged ) {
70- if ( value === 0 ) {
71- this . muted = true ;
72- } else if ( this . muted ) {
73- this . muted = false ;
74- } else {
75- this . audio . volume = value ;
76- }
77-
78- this . wf . invoke ( 'volumeChanged' , [ this . volume ] ) ;
72+ this . wf . invoke ( 'volumeChange' , [ value ] ) ;
7973 }
8074 }
8175 }
@@ -97,12 +91,13 @@ export class Player extends Destructable {
9791 }
9892
9993 get muted ( ) {
100- return this . audio ?. muted ?? false ;
94+ return this . audio ?. volume === 0 ;
10195 }
10296
10397 set muted ( muted : boolean ) {
10498 if ( ! this . audio ) return ;
105- if ( this . muted === muted ) return ;
99+
100+ if ( this . audio . muted === muted ) return ;
106101
107102 if ( muted ) {
108103 this . audio . mute ( ) ;
@@ -139,14 +134,11 @@ export class Player extends Destructable {
139134
140135 handleEnded = ( ) => {
141136 if ( this . loop ) return ;
142- this . updateCurrentTime ( true ) ;
143- } ;
144-
145- private playEnded ( ) {
146137 this . ended = true ;
138+ this . updateCurrentTime ( true ) ;
147139 this . pause ( ) ;
148140 this . wf . invoke ( 'playend' ) ;
149- }
141+ } ;
150142
151143 pause ( ) {
152144 if ( this . isDestroyed || ! this . playing || ! this . audio ) return ;
@@ -191,7 +183,7 @@ export class Player extends Destructable {
191183 this . timestamp = performance . now ( ) ;
192184 this . recreateSource ( ) ;
193185
194- if ( ! this . audio ) return ;
186+ if ( ! this . audio ?. source ) return ;
195187
196188 this . playing = true ;
197189
@@ -204,12 +196,8 @@ export class Player extends Destructable {
204196 start = clamp ( this . loop . start , 0 , duration ) ;
205197 }
206198
207- if ( this . audio . el ) {
208- this . audio . el . currentTime = this . currentTime ;
209- this . audio . el . addEventListener ( 'ended' , this . handleEnded ) ;
210- this . audio . el . play ( ) ;
211- }
212-
199+ this . audio . source . start ( 0 , start ?? 0 , duration ?? this . duration ) ;
200+ this . audio . source . addEventListener ( 'ended' , this . handleEnded ) ;
213201 this . watch ( ) ;
214202 }
215203
@@ -253,16 +241,16 @@ export class Player extends Destructable {
253241 private disconnectSource ( ) {
254242 if ( this . isDestroyed || ! this . audio || ! this . connected ) return ;
255243 this . connected = false ;
256-
257- if ( this . audio . el ) {
258- this . audio . el . removeEventListener ( 'ended' , this . handleEnded ) ;
259- }
244+ this . audio . source ?. removeEventListener ( 'ended' , this . handleEnded ) ;
245+ this . audio . source ?. stop ( 0 ) ;
260246 this . audio . disconnect ( ) ;
261247 }
262248
263249 private cleanupSource ( ) {
264250 if ( this . isDestroyed || ! this . audio ) return ;
265251 this . disconnectSource ( ) ;
252+
253+ delete this . audio . source ;
266254 delete this . audio ;
267255 }
268256
@@ -285,25 +273,18 @@ export class Player extends Destructable {
285273 }
286274 }
287275
288- private updateCurrentTime ( forceEnd = false ) {
276+ private updateCurrentTime ( forceTimeToEnd ?: boolean ) {
289277 const now = performance . now ( ) ;
290- const tick = ( ( now - this . timestamp ) / 1000 ) * this . rate ;
278+ const tick = ( ( now - this . timestamp ) / 1000 ) * this . rate ;
291279
292280 this . timestamp = now ;
293281
294282 const end = this . loop ?. end ?? this . duration ;
295283
296- const newTime = forceEnd ? this . duration : clamp ( this . time + tick , 0 , end ) ;
284+ const newTime = forceTimeToEnd ? this . duration : clamp ( this . time + tick , 0 , end ) ;
297285
298286 this . time = newTime ;
299-
300- if ( ! this . loop && this . time >= this . duration - tick ) {
301- this . time = this . duration ;
302- this . wf . invoke ( 'playing' , [ this . duration ] ) ;
303- this . playEnded ( ) ;
304- } else {
305- this . wf . invoke ( 'playing' , [ this . time ] ) ;
306- }
287+ this . wf . invoke ( 'playing' , [ this . time ] ) ;
307288 }
308289
309290 private stopWatch ( ) {
0 commit comments