Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Playing Sound in Javascript?

Posted on 1998-09-21
7
Medium Priority
?
322 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
[X]
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
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 100 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
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!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

618 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