1

I play audio via JavaScript in the following way:

this.audio = new Audio();
this.audio.src = "path/to/audio.wav";
this.audio.play();

The file audio.wav on the server will get larger with time and I would like the audio to keep playing without interruptions. However, it will stop playing once it reached the point of the initial file size.

What can I do to make it play seamlessly?

basilikum
  • 9,714
  • 4
  • 38
  • 55

1 Answers1

1

I am pretty sure this depends a lot on your server configuration.

WAVE format (in pretty much all of its flavors, including Microsoft and RIFF) has a field with the data size on top of the data block. This is read by the audio element especially if the preload attribute is set to blank, auto or metadata

Thus, the web browser making a request for such file might only request that many bytes and then just close the connection. There's nothing in the API description that says it'll read until actual end of file is met.

One thing that comes to mind, though, is registering on the "end" event. Then, according to this answer, you should be able to start playing again, seeking to the position you've ended on. The server should start streaming the data from the position you need. If it doesn't work directly, I'd try reinserting the audio node with preload="metadata" so that it could read the updated file size.

Another, perhaps simpler thing would be hacking the file size to be something absurdly big. With some luck, it might "just work".

Community
  • 1
  • 1
Bartek Banachewicz
  • 36,624
  • 6
  • 81
  • 129
  • Thank you, it seems there is much more to it, than I initially thought. For me, settings `currentTime` doesn't really work. Maybe because the byte range that is returned by Apache already extends up to the total length, so I cannot even set `currentTime` to a higher value than the length of the first fragment. I probably need to "teach" Apache to consider the response as incomplete, even though it delivers the full file? – basilikum Oct 26 '15 at 11:54
  • @basilikum That's the very last paragraph of my answer. – Bartek Banachewicz Oct 26 '15 at 12:00
  • True. My understanding is, that I should be somehow able to change the `Content-Range` header of Apache to have an unknown (or very large) total length. I actually posted a question on Serverfault about that (http://serverfault.com/q/731555/180374). It just doesn't seem to work (the header just doesn't get changed). Do you have any advice how I can achieve this? – basilikum Oct 26 '15 at 13:20