4

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?

2 Answers 2

3

You can't play mp3 files in firefox, it does not support them, you need an ogg version for firefox. Unless that changes, good to keep in mind.

Why doesn't Firefox support the MP3 file format in <audio>

2

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');

Not the answer you're looking for? Browse other questions tagged or ask your own question.