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?
You need to append the audio element to the an existing element.
This would be something like
Idealy, this should be done before you add the event listener, but after setting all the attributes
Also try
audio/mp3instead:audioElement.setAttribute(‘type’, ‘audio/mp3’);