@@ -264,19 +264,46 @@ export class AudioService {
264
264
265
265
// Don't process if audio is in error state
266
266
if ( this . audio . error ) {
267
- console . warn ( ' Skipping operation due to audio error' ) ;
267
+ console . warn ( " Skipping operation due to audio error" ) ;
268
268
return ;
269
269
}
270
270
271
271
const operation = this . pendingOperations . shift ( ) ;
272
-
272
+
273
273
try {
274
274
this . sourceBuffer . appendBuffer ( operation . chunk ) ;
275
- operation . resolve ( ) ;
275
+
276
+ // Set up event listeners
277
+ const onUpdateEnd = ( ) => {
278
+ operation . resolve ( ) ;
279
+ this . sourceBuffer . removeEventListener ( "updateend" , onUpdateEnd ) ;
280
+ this . sourceBuffer . removeEventListener (
281
+ "updateerror" ,
282
+ onUpdateError
283
+ ) ;
284
+ // Process the next operation
285
+ this . processNextOperation ( ) ;
286
+ } ;
287
+
288
+ const onUpdateError = ( event ) => {
289
+ operation . reject ( event ) ;
290
+ this . sourceBuffer . removeEventListener ( "updateend" , onUpdateEnd ) ;
291
+ this . sourceBuffer . removeEventListener (
292
+ "updateerror" ,
293
+ onUpdateError
294
+ ) ;
295
+ // Decide whether to continue processing
296
+ if ( event . name !== "InvalidStateError" ) {
297
+ this . processNextOperation ( ) ;
298
+ }
299
+ } ;
300
+
301
+ this . sourceBuffer . addEventListener ( "updateend" , onUpdateEnd ) ;
302
+ this . sourceBuffer . addEventListener ( "updateerror" , onUpdateError ) ;
276
303
} catch ( error ) {
277
304
operation . reject ( error ) ;
278
305
// Only continue processing if it's not a fatal error
279
- if ( error . name !== ' InvalidStateError' ) {
306
+ if ( error . name !== " InvalidStateError" ) {
280
307
this . processNextOperation ( ) ;
281
308
}
282
309
}
@@ -364,14 +391,14 @@ export class AudioService {
364
391
this . controller . abort ( ) ;
365
392
this . controller = null ;
366
393
}
367
-
394
+
368
395
if ( this . audio ) {
369
396
this . audio . pause ( ) ;
370
- this . audio . src = '' ;
397
+ this . audio . src = "" ;
371
398
this . audio = null ;
372
399
}
373
400
374
- if ( this . mediaSource && this . mediaSource . readyState === ' open' ) {
401
+ if ( this . mediaSource && this . mediaSource . readyState === " open" ) {
375
402
try {
376
403
this . mediaSource . endOfStream ( ) ;
377
404
} catch ( e ) {
@@ -380,25 +407,29 @@ export class AudioService {
380
407
}
381
408
382
409
this . mediaSource = null ;
383
- this . sourceBuffer = null ;
410
+ if ( this . sourceBuffer ) {
411
+ this . sourceBuffer . removeEventListener ( "updateend" , ( ) => { } ) ;
412
+ this . sourceBuffer . removeEventListener ( "updateerror" , ( ) => { } ) ;
413
+ this . sourceBuffer = null ;
414
+ }
384
415
this . serverDownloadPath = null ;
385
416
this . pendingOperations = [ ] ;
386
417
}
387
418
388
419
cleanup ( ) {
389
420
if ( this . audio ) {
390
421
this . eventListeners . forEach ( ( listeners , event ) => {
391
- listeners . forEach ( callback => {
422
+ listeners . forEach ( ( callback ) => {
392
423
this . audio . removeEventListener ( event , callback ) ;
393
424
} ) ;
394
425
} ) ;
395
-
426
+
396
427
this . audio . pause ( ) ;
397
- this . audio . src = '' ;
428
+ this . audio . src = "" ;
398
429
this . audio = null ;
399
430
}
400
431
401
- if ( this . mediaSource && this . mediaSource . readyState === ' open' ) {
432
+ if ( this . mediaSource && this . mediaSource . readyState === " open" ) {
402
433
try {
403
434
this . mediaSource . endOfStream ( ) ;
404
435
} catch ( e ) {
@@ -407,7 +438,11 @@ export class AudioService {
407
438
}
408
439
409
440
this . mediaSource = null ;
410
- this . sourceBuffer = null ;
441
+ if ( this . sourceBuffer ) {
442
+ this . sourceBuffer . removeEventListener ( "updateend" , ( ) => { } ) ;
443
+ this . sourceBuffer . removeEventListener ( "updateerror" , ( ) => { } ) ;
444
+ this . sourceBuffer = null ;
445
+ }
411
446
this . serverDownloadPath = null ;
412
447
this . pendingOperations = [ ] ;
413
448
}
0 commit comments