Skip to content

Instantly share code, notes, and snippets.

@vietkute02
Created August 31, 2019 08:50
Show Gist options
  • Save vietkute02/c344101d522f7ac4c8f7ec6aa301d105 to your computer and use it in GitHub Desktop.
Save vietkute02/c344101d522f7ac4c8f7ec6aa301d105 to your computer and use it in GitHub Desktop.
recover media error in hls
video.addEventListener('error', handleVideoEvent);
function handleVideoEvent(evt) {
let data = '';
switch(evt.type) {
case 'error':
data = Math.round(evt.target.currentTime*1000);
if(evt.type === 'error') {
let errorTxt, mediaError=evt.currentTarget.error;
switch(mediaError.code) {
case mediaError.MEDIA_ERR_ABORTED:
errorTxt = 'You aborted the video playback';
break;
case mediaError.MEDIA_ERR_DECODE:
errorTxt = 'The video playback was aborted due to a corruption problem or because the video used features your browser did not support';
handleMediaError();
break;
case mediaError.MEDIA_ERR_NETWORK:
errorTxt = 'A network error caused the video download to fail part-way';
break;
case mediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
errorTxt = 'The video could not be loaded, either because the server or network failed or because the format is not supported';
break;
}
if (mediaError.message) {
errorTxt += ' - ' + mediaError.message;
}
logStatus(errorTxt);
console.error(errorTxt);
}
break;
default:
break;
}
function handleMediaError() {
if(autoRecoverError) {
let now = performance.now();
if(!recoverDecodingErrorDate || (now - recoverDecodingErrorDate) > 3000) {
recoverDecodingErrorDate = performance.now();
$('#statusOut').append(', trying to recover media error.');
hls.recoverMediaError();
} else {
if(!recoverSwapAudioCodecDate || (now - recoverSwapAudioCodecDate) > 3000) {
recoverSwapAudioCodecDate = performance.now();
$('#statusOut').append(', trying to swap audio codec and recover media error.');
hls.swapAudioCodec();
hls.recoverMediaError();
} else {
$('#statusOut').append(', cannot recover. Last media error recovery failed.');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment