Solved

Playing Sound in Javascript?

Posted on 1998-09-21
7
278 Views
Last Modified: 2008-02-01
How would I go about playing sound on demand via Javascript?

What I want to do is to have the user click a button (10 different buttons) and for Javascript to fetch that sound from the server much like a graphical image with "document.image[0].src".  Is there any way to do such a thing with Javascript for Netscape 3+ and MS-Explorer?

Thanks!
0
Comment
Question by:ratboy2
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
ID: 1271177
You have so many different players out there so my suggestion is to simply write the html to a hidden frame:

<HTML>
<HEAD>
<SCRIPT>
ns = navigator.appName.indexOf('Netscape');
function playsound(soundname) {
   var Text=''
   if ns Text += '<EMBED SRC="' + soundname + '">';
   else Text += '<BODY BGSOUND="' + soundname + '">';
   top.hidden.document.write(Text);
</SCRIPT>
</HEAD>
<FRAMESET ROWS="100%,*">
<FRAME NAME="content" SRC="content.html">
<FRAME NAME="hidden" SRC="javascript:' '">
</FRAMSET>
</HTML>

and in your pages:
<FORM>
<INPUT TYPE="BUTTON" VALUE="arrggh" onClick="top.playSound('arrrgh.wav');>
<INPUT TYPE="BUTTON" VALUE="Rap" onClick="top.playSound('rap.mid');>
</FORM>

and so on
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1271178
PS: You can only preload sounds by loading them into a page - there is not yet a sound.src. You can however do that wiht Java but that is a different kettle of fish (and another forum)

Michel
0
 
LVL 4

Expert Comment

by:martinag
ID: 1271179
I don't know if there is any function call in JS to start a sound, but you could use a hidden frame:

<SCRIPT LANGUAGE="JavaScript">
<!--
function sound(s) {
  parent.hiddenFrame.open();
  parent.hiddenFrame.write('<EMBED SRC="' + s +'" HIDDEN="TRUE" AUTOSTART="TRUE"');
  parent.hiddenFrame.close();
}
// -->
</SCRIPT>
<BUTTON VALUE="Sound 1" onClick="sound('sound1.wav')">

Martin
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:martinag
ID: 1271180
Always forget to reload before posting!
0
 
LVL 4

Expert Comment

by:martinag
ID: 1271181
Just noticed:
if ns Text += '<EMBED SRC="' + soundname + '">';
else Text += '<BODY BGSOUND="' + soundname + '">';

IE recognizes the <EMBED> tag, so this will not be neccesary.
Text += '<EMBED SRC="' + soundname + '">';
will be fine.

Martin
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1271182
IE4 recognises the embed, ie3 does not.

Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1271183
martinag: The various players out there almost all support javascript control
but it is a pain to figure out which player, if any the user has.

Michel
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now