views:

66

answers:

1

Doesn't matter what I do, I simply cant get this to play a sound in firefox or IE, of Chrome for that matter.

<html>
<head>
<script type="text/javascript">

    function play() 
 {
     var embed = document.createElement('object');

     embed.setAttribute('src', 'c:\\test.wav');
     embed.setAttribute('hidden', true);
     embed.setAttribute('autostart', true);
     embed.setAttribute('enablejavascript', true);

     document.childNodes[0].appendChild(embed);

 }

// -->
</script>
</head>
<body onload="play();">
</body>
</html>
+1  A: 

Try using this revised version of the function play()

function play() 
{
  var embed=document.createElement('object');
  embed.setAttribute('type','audio/wav');
  embed.setAttribute('data', 'c:\test.wav');
  embed.setAttribute('autostart', true);
  document.getElementsByTagName('body')[0].appendChild(embed);
}

The problem with your code was you were using the src attribute, which is for the <embed> tag. Instead, use the data attribute for the <object> tag.



If you are trying to get the most compatibility out of this, you should also consider adding the embed tag as an alternate for the object tag. The way it works is like this:

<object data="test.wav" type="audio/wav" autostart="true">
<embed src="test.wav" autostart="true" alt="Could not load audio" />
</object>

This works similar to the noscript tag, where older browsers that don't support the object tag resort to the embed tag.

Kranu
Nope, that also doesn't seem to play the file....
FaNIX
Are you sure that you have a valid WAV file? I tested my code on my computer before posting it, and it worked flawlessly on Google Chrome. Try placing the test.wav file in the same directory as the html file and just setting the data attribute to 'test.wav'. In addition, could you let me know which browser you're using so I can check myself?
Kranu
Okay, I moved the file into the same folder, it plays with google chrome, but not with firefox. I actually only need it to work in firefox.
FaNIX
I'm glad it works on Google Chrome for you now. I think the reason for it not playing on firefox would be due to security restrictions. For example, when I run the code locally from my computer, it doesn't work. However, I uploaded it http://goo.gl/gjjd and it seems to work fine (with a slight delay because of the host). Let me know what happens for you.
Kranu
Thanks mate, that worked...
FaNIX