kdpnz
kdpnz

Reputation: 638

Set MIME type of Javascript audio object

To support HLS audio streaming on a HTML5 Audio tag, we can successfully use this MIME type:

<audio autoplay>
    <source src="AUDIO_URL" type="application/x-mpegURL">
</audio>

However if I create the audio element programmatically, I can't seem to specify what the MIME type should be. Because I'm not able to provide that specific MIME type for HLS audio streaming (m3u8 extension) the audio playback fails.

How can I specify a MIME type when creating a new Audio() in code?

Upvotes: 0

Views: 2048

Answers (1)

chrisguttandin
chrisguttandin

Reputation: 9116

It is possible to build up the same structure that you have in your HTML with JavaScript.

const audio = new Audio();
const source = document.createElement('source');

source.setAttribute('src', 'AUDIO_URL');
source.setAttribute('type', 'application/x-mpegURL');

audio.append(source);
audio.play();

Upvotes: 1

Related Questions