You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/API.md
+28-6Lines changed: 28 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -350,22 +350,44 @@ hls.on(Hls.Events.ERROR, function (event, data) {
350
350
351
351
#### Fatal Error Recovery
352
352
353
-
HLS.js provides several methods for attempting playback recover in the event of a decoding error in the HTMLMediaElement:
353
+
HLS.js provides methods for attempting playback recover in the event of a decoding error in the HTMLMediaElement:
354
354
355
355
##### `hls.recoverMediaError()`
356
356
357
-
Resets the MediaSource and restarts streaming from the last known playhead position.
357
+
Resets the MediaSource and restarts streaming from the last known playhead position. This should only be used when the media element is in an error state.
358
+
It should not be used in response to non-fatal hls.js error events.
358
359
359
360
###### Error recovery sample code
360
361
361
362
```js
362
-
hls.on(Hls.Events.ERROR, function (event, data) {
363
+
let attemptedErrorRecovery =null;
364
+
365
+
video.addEventListener('error', (event) {
366
+
constmediaError=event.currentTarget.error;
367
+
if (mediaError.code===mediaError.MEDIA_ERR_DECODE) {
368
+
constnow=Date.now();
369
+
if (!attemptedErrorRecovery || now - attemptedErrorRecovery >5000) {
370
+
attemptedErrorRecovery = now;
371
+
hls.recoverMediaError();
372
+
}
373
+
}
374
+
});
375
+
376
+
hls.on(Hls.Events.ERROR, function (name, data) {
377
+
// Special handling is only needed to errors flagged as `fatal`.
363
378
if (data.fatal) {
364
379
switch (data.type) {
365
-
caseHls.ErrorTypes.MEDIA_ERROR:
366
-
console.log('fatal media error encountered, try to recover');
367
-
hls.recoverMediaError();
380
+
caseHls.ErrorTypes.MEDIA_ERROR: {
381
+
constnow=Date.now();
382
+
if (!attemptedErrorRecovery || now - attemptedErrorRecovery >5000) {
383
+
console.log('Fatal media error encountered ('+video.error++'), attempting to recover');
384
+
attemptedErrorRecovery = now;
385
+
hls.recoverMediaError();
386
+
} else {
387
+
console.log('Skipping media error recovery (only '+ (now - attemptedErrorRecovery) +'ms since last error)');
0 commit comments