Detecting HTML5 audio element file not found error

前端 未结 3 1504
暗喜
暗喜 2020-12-30 08:01

I am trying to make a the browser display an alert if an audio element src attribute points to a non existent file, however I do not get any response if I attach the the \"

3条回答
  •  误落风尘
    2020-12-30 08:49

    Getting audio errors

    $('audio').addEventListener('error', function failed(e) {
       // audio playback failed - show a message saying why
       // to get the source of the audio element use $(this).src
       switch (e.target.error.code) {
         case e.target.error.MEDIA_ERR_ABORTED:
           alert('You aborted the video playback.');
           break;
         case e.target.error.MEDIA_ERR_NETWORK:
           alert('A network error caused the audio download to fail.');
           break;
         case e.target.error.MEDIA_ERR_DECODE:
           alert('The audio playback was aborted due to a corruption problem or because the video used features your browser did not support.');
           break;
         case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
           alert('The video audio not be loaded, either because the server or network failed or because the format is not supported.');
           break;
         default:
           alert('An unknown error occurred.');
           break;
       }
     }, true);
    

    Quoted from How to check if HTML5 audio has reached different errors

提交回复
热议问题