Solved

Playing Sound in Javascript?

Posted on 1998-09-21
7
295 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

685 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