Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Playing Sound in Javascript?

Posted on 1998-09-21
7
290 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

809 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