Create a html5 audio and play it doesn't work

audio, dynamic, html

Solution

You need to append the audio element to the an existing element. This would be something like

document.getElementById("my_audio_div").appendChild(audioElement);

Idealy, this should be done before you add the event listener, but after setting all the attributes

Also try `audio/mp3` instead: audioElement.setAttribute('type', 'audio/mp3');

Problem

I want create a html5 audio in dynamic and play it,here is the code: ``` function playAnotherMusic(playUrl){ var audioElement = document.createElement('audio'); audioElement.setAttribute('src', playUrl); audioElement.setAttribute('controls', true); audioElement.setAttribute('preload', true); audioElement.setAttribute('type', 'audio/mpeg'); audioElement.addEventListener("load", function() { audioElement.play(); }, true); console.log(playUrl); audioElement.load(); } ``` However it doesn't work,the firebug assigin me "`HTTP "Content-Type" of "audio/mpeg" is not supported.`" how can I solve this problem?

Original source

Related problems