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

113
Visualizações
How to play audio loaded via file input

I want to load an audio file via input file reader and then be able to play it ...

Here is the process of creating file blob and assign the audio source to this blob:

    fileInput.on('change', readFile);

    function readFile(e) {
        const input = e.target;
        if(input.files && input.files[0]) {
            const reader = new FileReader();
            reader.onload = (e) => {
                const binaryData = e.target.result;
                const binary = convertDataURIToBinary(binaryData); 
                const blob = new Blob(binary, {type: input.files[0].type });
                console.log('inserting blobXX', blob);
                recordedAudioComponent.src = URL.createObjectURL(blob);             
                data.instructAudios[component] = blob;
                console.log('recordedAudioComponent.src', recordedAudioComponent.src);
            
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

And here is the result of above code on console:

enter image description here

Now I'm trying to play the recordedAudioComponent which is an audio tag with the source right?

const recordedAudioComponent = document.getElementById(`recordedAudio-instruct-${component}`);
console.log(recordedAudioComponent)
recordedAudioComponent.play();

And here is the error it produces:

enter image description here

How can I fix this and play the audio?

EDIT: Here is the function to get the blob in the first code:

   var BASE64_MARKER = ';base64,';

    function convertDataURIToBinary(dataURI) {
      var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
      var base64 = dataURI.substring(base64Index);
      var raw = window.atob(base64);
      var rawLength = raw.length;
      var array = new Uint8Array(new ArrayBuffer(rawLength));

      for(i = 0; i < rawLength; i++) {
        array[i] = raw.charCodeAt(i);
      }
      return array;
    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You don't need any of convertion, blob is actually pretty similar from the file itself and the method URL.createObjectURL accepts file as argument, all you need to do to play the audio file from your browser is set the src attribute as the url created, something like this:

<body>
    <input style="display: block;" type="file" onchange="loadAudioFile(this)">
    <audio src=" " id="track" controls>
    </audio>
    <script>

        function loadAudioFile(e) {
            const url = URL.createObjectURL(e.files[0]);
            document.getElementById('track').setAttribute('src', url);
        }


    </script>
</body>

about 4 years ago · Juan Pablo Isaza Relatório

0

<input type="file" name="fileInput" id="fileInput">
<script>
    const fileInput = document.querySelector('#fileInput');
    fileInput.onchange = readFile;

    /** @type {AudioNode} */
    const audio = document.createElement( 'audio' );

    function readFile(e) {
        const input = e.target;
        if(input.files && input.files[0]) {
            const reader = new FileReader();
            reader.onloadend = function (e){
                audio.src = e.target.result;
                audio.play();
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
</body>
about 4 years ago · Juan Pablo Isaza Relatório
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