@@ -27,9 +27,9 @@ export class Player extends Destructable {
27
27
* Get current playback speed
28
28
*/
29
29
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
33
33
}
34
34
}
35
35
@@ -44,8 +44,8 @@ export class Player extends Destructable {
44
44
45
45
this . _rate = value ;
46
46
47
- if ( this . audio ) {
48
- this . audio . speed = value ;
47
+ if ( this . audio ?. source ) {
48
+ this . audio . source . playbackRate . value = value ;
49
49
50
50
if ( rateChanged ) {
51
51
this . wf . invoke ( 'rateChanged' , [ value ] ) ;
@@ -54,28 +54,22 @@ export class Player extends Destructable {
54
54
}
55
55
56
56
get duration ( ) {
57
- return this . audio ?. duration ?? 0 ;
57
+ return this . audio ?. buffer ?. duration ?? 0 ;
58
58
}
59
59
60
60
get volume ( ) {
61
- return this . audio ?. volume ?? 1 ;
61
+ return this . audio ?. gain ?. gain . value ?? 1 ;
62
62
}
63
63
64
64
set volume ( value : number ) {
65
65
if ( this . audio ) {
66
66
67
67
const volumeChanged = this . volume !== value ;
68
68
69
+ this . audio . volume = value ;
70
+
69
71
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 ] ) ;
79
73
}
80
74
}
81
75
}
@@ -97,12 +91,13 @@ export class Player extends Destructable {
97
91
}
98
92
99
93
get muted ( ) {
100
- return this . audio ?. muted ?? false ;
94
+ return this . audio ?. volume === 0 ;
101
95
}
102
96
103
97
set muted ( muted : boolean ) {
104
98
if ( ! this . audio ) return ;
105
- if ( this . muted === muted ) return ;
99
+
100
+ if ( this . audio . muted === muted ) return ;
106
101
107
102
if ( muted ) {
108
103
this . audio . mute ( ) ;
@@ -139,14 +134,11 @@ export class Player extends Destructable {
139
134
140
135
handleEnded = ( ) => {
141
136
if ( this . loop ) return ;
142
- this . updateCurrentTime ( true ) ;
143
- } ;
144
-
145
- private playEnded ( ) {
146
137
this . ended = true ;
138
+ this . updateCurrentTime ( true ) ;
147
139
this . pause ( ) ;
148
140
this . wf . invoke ( 'playend' ) ;
149
- }
141
+ } ;
150
142
151
143
pause ( ) {
152
144
if ( this . isDestroyed || ! this . playing || ! this . audio ) return ;
@@ -191,7 +183,7 @@ export class Player extends Destructable {
191
183
this . timestamp = performance . now ( ) ;
192
184
this . recreateSource ( ) ;
193
185
194
- if ( ! this . audio ) return ;
186
+ if ( ! this . audio ?. source ) return ;
195
187
196
188
this . playing = true ;
197
189
@@ -204,12 +196,8 @@ export class Player extends Destructable {
204
196
start = clamp ( this . loop . start , 0 , duration ) ;
205
197
}
206
198
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 ) ;
213
201
this . watch ( ) ;
214
202
}
215
203
@@ -253,16 +241,16 @@ export class Player extends Destructable {
253
241
private disconnectSource ( ) {
254
242
if ( this . isDestroyed || ! this . audio || ! this . connected ) return ;
255
243
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 ) ;
260
246
this . audio . disconnect ( ) ;
261
247
}
262
248
263
249
private cleanupSource ( ) {
264
250
if ( this . isDestroyed || ! this . audio ) return ;
265
251
this . disconnectSource ( ) ;
252
+
253
+ delete this . audio . source ;
266
254
delete this . audio ;
267
255
}
268
256
@@ -285,25 +273,18 @@ export class Player extends Destructable {
285
273
}
286
274
}
287
275
288
- private updateCurrentTime ( forceEnd = false ) {
276
+ private updateCurrentTime ( forceTimeToEnd ?: boolean ) {
289
277
const now = performance . now ( ) ;
290
- const tick = ( ( now - this . timestamp ) / 1000 ) * this . rate ;
278
+ const tick = ( ( now - this . timestamp ) / 1000 ) * this . rate ;
291
279
292
280
this . timestamp = now ;
293
281
294
282
const end = this . loop ?. end ?? this . duration ;
295
283
296
- const newTime = forceEnd ? this . duration : clamp ( this . time + tick , 0 , end ) ;
284
+ const newTime = forceTimeToEnd ? this . duration : clamp ( this . time + tick , 0 , end ) ;
297
285
298
286
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 ] ) ;
307
288
}
308
289
309
290
private stopWatch ( ) {
0 commit comments