Search code examples
iosaudiosafarihtml5-audioaudio-streaming

iOS Safari audio stream play issue


So I'm streaming an .mp3 audio from my nodejs-server (using stream.pipe(response)), and playing it using HTML5 <audio>. Works fine on all devices except iOS browsers. The typical behaviour: audio is playing a few seconds and then pauses and I can't even unpause it so I have to refresh the page. But I chagnes nothing cause anyway audio stops again after a few seconds of playing.

Strange thing: It works perfectly on localhost.

Even if it's just a connection speed problem, I think browser still should be able to somehow buffer the audio and play it as soon as it's ready.

Response headers:

        res.status(206).header({
            'Content-Type': 'audio/mpeg',
            'Content-Length': content_length,
            'Content-Range': "bytes " + start + "-" + end + "/" + stat.size,
            'Content-Transfer-Encoding': "binary",
            'Accept-Ranges': "bytes"
        });

        readStream = fs.createReadStream(mp3path, {
            start: start,
            end: end
        });
        readStream.pipe(res);

The webpage code is simple, I just use an <audio> tag and set the src from js file:

htmlAudio.src = audio_stream_api_link;

Solution

  • Okay, so if anyone will ever struggle with the same problem, in my case the solution was to fix proxy (nginx) configuration by adding:

     proxy_set_header Connection keep-alive; 
    

    Ref