Home > Software design >  Sending audio to client using .pipe(res) doesn't allow you to seek?
Sending audio to client using .pipe(res) doesn't allow you to seek?

Time:07-09

If I pipe'd an mp3 file through an express.js server like this:

createReadStream('media/mp3file.mp3').pipe(res)

It sends the audio data but I cannot seek. It just quickly pauses the audio, moves the seek bar back to the current time and then resumes the audio.

Here is a GIF:

1

How do I make it so I can seek forwards and backwards?

CodePudding user response:

Streaming file data from the server doesn't allow for seeking because it's a one-off operation from the context of the server.

To enable seeking the server needs to allow HTTP range requests, where a client tells it that it wants to receive a part of a download (starting at byte X, ending at byte Y, the "range" part).

Express support such requests when you use res.sendFile() to send the file data.

If you want to render a web page with an audio player, you need to add (at least) two handlers: one for the HTML file, and one for the audio file. If you're just using regular plain HTML files, you can use the express.static() middleware, which you point to a directory that contains your HTML files.

  • Related