Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Modify download file name in html5 audio tag

I am working with an audio tag whose source can be one of several types but called from an ashx file, as I am dynamically loading the stream.

<audio id="audio" controls="" autoplay="" download="soundFile.wav">
  <source id="mp3" src="../Controls/SoundPlayer/GetSoundFile.ashx?soundType=Sound&amp;fileId=test&amp;fileType=mp3" type="audio/mpeg3">
  <source id="wav" src="../Controls/SoundPlayer/GetSoundFile.ashx?soundType=Sound&amp;fileId=test&amp;fileType=wav" type="audio/wav">
</audio>

This part works fine, but what I'd like to do is catch an error when it fails. Which I can do like so:

$('audio').on('error', function (e) {
    $('#divAudioPlayerMessage').text('Recording can’t be played');
});
$('source').on('error', function (e) {
    $('#divAudioPlayerMessage').text('Recording can’t be played');
});

Or like so (which apparently really only works in IE [I got this from How to check if HTML5 audio has reached different errors)

document.getElementsByTagName('audio')[0].addEventListener('error', function(e, o) {
    // audio playback failed - show a message saying why
    // to get the source of the audio element use $(this).src
    var errorMessage = '';
    switch (e.target.error.code) {
        case e.target.error.MEDIA_ERR_ABORTED:
            errorMessage = 'You aborted the video playback.';
            break;
        case e.target.error.MEDIA_ERR_NETWORK:
            errorMessage = 'A network error caused the audio download to fail.';
            break;
        case e.target.error.MEDIA_ERR_DECODE:
            errorMessage = '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:
            errorMessage = 'The video audio not be loaded, either because the server or network failed or because the format is not supported.';
            break;
        default:
            errorMessage = 'An unknown error occurred.';
            break;
    }
    $('#divAudioPlayerMessage').text(errorMessage);
}, true);

My question is how do I tell what the actual error is. For example, in my ashx file I might return a 404 error, which if I get I would like to give a specific message.

As well, my other problem, is that when running in Chrome or Firefox, who do not support mp3, my error handler will be called, but I would like to ignore that error since I also have a wav file which is supported.

Thank you!

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda