Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nightwolf

macrumors member
Original poster
Oct 4, 2005
33
0
Ontario, CANADA
I really need music or audio codes that will work on windows and macs. These are the codes I have been using and they do not seem to work that well on both platforms.

1. <BGSOUND balance=0 src="music/song.mp3" volume=0 loop=infinite autostart="true"></FONT></A><br>

2. <embed src="music/Hero of the day 16.mp3" autostart="true" loop="true" width="2" height="0">
</embed>

I really need these codes. Please help!
:confused:
 
Here is some JavaScript code I have from a web game I created. It was a while back, but I believe it worked in IE, Firefox, Safari, and Opera. I had only tried it with .wav files from what I remember. You'll just have to try out the mp3 to see if it works. Should also work for midi files.

The "s" that get passed to the function is a string that holds the destination of the file (e.g. "music/soundfile.wav"). It dynamically inserts a sound file into the page then deletes itself after 2 seconds. This can be modified to meet your needs though.

Code:
function soundPlay(s)
{
  if(window.opera) soundPlayOpera(s);
  sound = document.createElement("embed");
  sound.setAttribute("src", s);
  sound.setAttribute("id", "sfx");
  sound.setAttribute("hidden", true);
  sound.setAttribute("autostart", true);
  document.body.appendChild(sound);
  // pause long enough to play sfx, then delete itself
  setTimeout("removeSound()",2000);
}
function soundPlayOpera(sfx)
{
  a = new Audio(sfx);
  a.play();
}
function removeSound()
{
  d = document.getElementById("sfx");
  d.parentNode.removeChild(d);
}

Playing music on web pages doesn't have great support from HTML alone. The alternative is generally Flash. Check this article out for some details about your options.

http://www.boutell.com/newfaq/creating/audio.html
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.