Skip to content

Commit 84e4fbd

Browse files
authored
Improve recoverMediaError documentation in API.md (#7447)
1 parent a19f424 commit 84e4fbd

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

docs/API.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,44 @@ hls.on(Hls.Events.ERROR, function (event, data) {
350350

351351
#### Fatal Error Recovery
352352

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:
354354

355355
##### `hls.recoverMediaError()`
356356

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.
358359

359360
###### Error recovery sample code
360361

361362
```js
362-
hls.on(Hls.Events.ERROR, function (event, data) {
363+
let attemptedErrorRecovery = null;
364+
365+
video.addEventListener('error', (event) {
366+
const mediaError = event.currentTarget.error;
367+
if (mediaError.code === mediaError.MEDIA_ERR_DECODE) {
368+
const now = 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`.
363378
if (data.fatal) {
364379
switch (data.type) {
365-
case Hls.ErrorTypes.MEDIA_ERROR:
366-
console.log('fatal media error encountered, try to recover');
367-
hls.recoverMediaError();
380+
case Hls.ErrorTypes.MEDIA_ERROR: {
381+
const now = 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)');
388+
}
368389
break;
390+
}
369391
case Hls.ErrorTypes.NETWORK_ERROR:
370392
console.error('fatal network error encountered', data);
371393
// All retries and media options have been exhausted.

0 commit comments

Comments
 (0)