0

I'm trying to play a wav file by ANY means. With the audio tag or not, it does not matter.

It's for an application only used internally, and the sound only plays when a serious error is made. So it exists to annoy people.

This is the code so far:

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'wav/actors_relig1.wav');
audioElement.play();

But that did not work.

Jelle De Loecker
  • 20,999
  • 27
  • 100
  • 142

1 Answers1

1

I think you need to append the created div first

document.getElementsByTagName('body')[0].appendChild(audioElement);

and wait for it to load with

audioElement.addEventListener('loadeddata', function() {audioElement.play()}, false);

Achshar
  • 5,153
  • 8
  • 40
  • 70
  • i am not really sure if it is `loadeddata` or not but i know for sure about `loadedmetadata` (which fires when, as the name suggests, meta data of the audio is loaded but song itself is not) so `loadeddata` makes sense, a quick google search didn't reveal anything but it should work :) – Achshar Jun 26 '11 at 19:10