Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
File data buffer does not contain the entire file?

I'm attempting to stream an mp3 file as response to a get request from the client. I got as far as receiving a buffer and making it play. However when I made the buffer play with the web audio API, I bumped into a problem; The song (almost?) never finishes, I logged the buffer I'm reading from, and for whatever reason, it appears that it's not the same size everytime (and as it should be), does anyone know why this could be the case?

My server code:

app.get(`/stest`, async (req, res) => {

    let f = fs.createReadStream(`./archive/Kill_Of_The_Night.mp3`)
    console.log(f)
    console.log(typeof(f))
    // res.send(f)
    // f.pipe(res)
    f.on('open', () => {
        console.log(`opened`)
        res.attachment(`KillOfTheNight.mp3`)
        f.pipe(res)
    })
    f.on('end', () => {
        console.log(`ended`)
    })
    f.on('error', (e) => {
        console.log(e)
    })

})

My client code:

    button.addEventListener('click', async () => {


        fetch(`/stest`).then(async (res) => {

            const audioContext = new AudioContext()

            console.log(res)


            let reader = res.body.getReader()
            console.log(reader)
            console.log(reader.closed)

            reader.read().then(({ value, done }) => {

                console.log(value.buffer)
                console.log(done)

                let b = undefined

                audioContext.decodeAudioData(value.buffer, (buffer) => {

                    b = buffer

                    let source = audioContext.createBufferSource()
                    source.buffer = b
                    source.connect(audioContext.destination)

                    source.start(0)

                }).catch((e) => {

                    console.log(e)

                })

            }).catch(() => {

                console.log(e)

            })



        }).catch((e) => {

            console.log(e)

        })

    })

Now when I log "value.buffer", I get a different size everytime, as visible in this screenshot.

Does anyone know I can make sure the entire file is within the buffer so that the audio does not cut off in the middle of the track?

Thanks for reading!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda