?
Solved

Playing Sound in Javascript?

Posted on 1998-09-21
7
Medium Priority
?
311 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

777 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