why is "how to play a sound with Javascript" such a rare topic?


J

Jake Barnes

I did a search on the newsgroup comp.lang.javascript. I was searching
for "how to play a sound with Javascript". I'm somewhat suprised that
the majority of entries are from the 1990s, and there are almost no
posts from the last 3 years:

http://groups.google.com/group/comp...+sound+with+Javascript&qt_g=Search+this+group

Even after sorting by date, there don't appear any entries more recent
than 2006, and there are only 3 from 2006.

If I were to chart the frequency of questions about sound on a graph,
it would appear there was great interest in the question during the
1990s, and then that interest faded away, disappearing almost entirely
after 2004.

Why is that? Did Flash become good enough and widespread enough that
programmers now rely on it completely when trying to get sound to play
on a site? Is there the sense that it is too difficult to use
Javascript to play a sound on a web page?

I was working on some Ajax chat software and I was asked to make a
sound play when someone posted a new message to a chat room. I was
thinking of doing this in a pure-Javascript way, but perhaps that is
to be advised against? Should I use Flash instead?

I asked my friends for a pure Javascript solution and they pointed me
to a script that still involves Flash:

http://www.boutell.com/newfaq/creating/scriptsound.html
 
Ad

Advertisements

D

David Mark

I did a search on the newsgroup comp.lang.javascript. I was searching
for "how to play a sound with Javascript". I'm somewhat suprised that
the majority of entries are from the 1990s, and there are almost no
posts from the last 3 years:

http://groups.google.com/group/comp.lang.javascript/search?group=comp...

Even after sorting by date, there don't appear any entries more recent
than 2006, and there are only 3 from 2006.

It is a rare Web page that needs to make use of sounds.
If I were to chart the frequency of questions about sound on a graph,
it would appear there was great interest in the question during the
1990s, and then that interest faded away, disappearing almost entirely
after 2004.

Why is that? Did Flash become good enough and widespread enough that
programmers now rely on it completely when trying to get sound to play
on a site? Is there the sense that it is too difficult to use
Javascript to play a sound on a web page?

Flash is not "good enough" for much of anything IMO. It is a rare Web
page that needs to make use of Flash.

There may be a sense that it is too difficult to play sounds with
JavaScript, but it is nonsense. The issues involved are virtually
identical to those involved with using Flash. The difference is that
there are tons of (incompetent) Flash scripts out there (most notably
the one provided by Adobe), but few that deal with audio plug-ins.
I was working on some Ajax chat software and I was asked to make a
sound play when someone posted a new message to a chat room. I was
thinking of doing this in a pure-Javascript way, but perhaps that is
to be advised against? Should I use Flash instead?

No, you had it right to begin with.
I asked my friends for a pure Javascript solution and they pointed me
to a script that still involves Flash:

http://www.boutell.com/newfaq/creating/scriptsound.html

Then they pointed you to an inappropriate script.

If you are writing an Ajax chat application, then you should be able
to tackle this. As with Flash, you need two branches, one that checks
the plugins and/or mimeTypes collections to determine support for your
chosen format and a fallback for IE and its various derivations (e.g.
old AOL browsers, which have no such collections (the feature
detection pattern should be obvious.)

The first branch adds an object element to the body to play a sound
and removes it after a suitable delay (which cannot be calculated
exactly as it takes time to download the file and load the plug-in.)
I use two object elements to allow for music and sounds to play
simultaneously. You can "preload" the plug-in with dummy (silent)
audio file(s) when the page loads, but still need to pad the duration
to allow time for the initial download.

The second branch adds a bgsound element (or two) to the head and
changes the src property to play sounds. You can actually preload the
sound files with this method as you can play sounds silently by
manipulating the volume property. This works much better than the
other method as you never have to remove the elements, so there is no
need to pad the play time.
 
H

Henry

I did a search on the newsgroup comp.lang.javascript. I
was searching for "how to play a sound with Javascript".
I'm somewhat suprised that the majority of entries are
from the 1990s, and there are almost no posts from the
last 3 years:
Why is that? ...
<snip>

You are describing a period in which there was a growing realisation
that there are almost no circumstances where it makes sense for a
public web page to make an involuntary noise. That is, we are at a
point (culturally and technologically) were people expect to exercise
control over the ambient/background/foreground noise in their
(particularly domestic) environments, leaving any additional noise
intrusive and unwelcome. Eventually things that are perceived as
unwelcome by users are perceived as being bad ideas by programmers.
 
S

SAM

Jake Barnes a écrit :
I asked my friends for a pure Javascript solution and they pointed me
to a script that still involves Flash:

http://www.boutell.com/newfaq/creating/scriptsound.html
It's the best.

Anyway it is impossible to play a sound in JS ...
a sound can only be played by a plug-in and there was so much plug-ins
to play sound whom each one with its proper JS commands, in these
conditions how to be sure to have thought to all of them?

Today it is not very important any more to use a heavy mp3 instead of
light midi
Flash play mp3 and can be driven via JS
Flash is present on 90% of home computers
so ... at this date ... it's the solution
 
D

David Mark

Jake Barnes a écrit :



It's the best.

How so?
Anyway it is impossible to play a sound in JS ...
a sound can only be played by a plug-in and there was so much plug-ins
to play sound whom each one with its proper JS commands, in these
conditions how to be sure to have thought to all of them?

You really don't have to consider the plug-in at all.
Today it is not very important any more to use a heavy mp3 instead of
light midi
Flash play mp3 and can be driven via JS

I didn't follow that, but you can certainly play MP3's using the
methods I described. In fact, they work best for longer clips as most
plug-ins will stream them.
Flash is present on 90% of home computers

That isn't a comforting statistic. And not all of those Flash
installations will support interaction with JavaScript.
so ... at this date ... it's the solution

Of course, there aren't any decent scripts out there for Flash.

Quoting the first two lines from the "SoundManager" script:

var isIE = navigator.appName.toLowerCase().indexOf('internet explorer')
+1;
var isMac = navigator.appVersion.toLowerCase().indexOf('mac')+1;

I stopped reading after that.

And it appears you have to convert every sound effect to MP3. On the
plus side, it is very responsive, but do you really need UI sounds in
a Web page? The OP just wants to sound a chime periodically.
 
K

Kevin Scholl

That isn't a comforting statistic. And not all of those Flash
installations will support interaction with JavaScript.

Nor does the presence of Flash necessarily mean that it's turned on. A
number of my colleagues (myself included) utilize a plugin that blocks
Flash from loading automatically (replacing it with the Flash icon so
we know it's there) unless we specifically click it to activate.
 
Ad

Advertisements

S

SAM

David Mark a écrit :
Oooops!
I thought we told of this one :
<http://www.schillmania.com/projects/soundmanager2/>
issued from :
<http://www.schillmania.com/projects/soundmanager/>
(perhaps are they all the same ?)

(snip)
Quoting the first two lines from the "SoundManager" script:

I've seen that too :-/
var isIE = navigator.appName.toLowerCase().indexOf('internet explorer')
+1;
var isMac = navigator.appVersion.toLowerCase().indexOf('mac')+1;

I stopped reading after that.

I only have a look to the code after having pushed some of proposed
buttons in soundManager_1 that did noises (yes they did !)
And it appears you have to convert every sound effect to MP3.

And then ?
The sound has to be coded anyway (even it is a midi), no ?
On the
plus side, it is very responsive, but do you really need UI sounds in
a Web page? The OP just wants to sound a chime periodically.

I don't say the OP has to have its page to download an elephant if he
just needs a mouse.
I say only that soundmanager is the best (the easiest way) to serve sounds.
There are some others Flash mp3 players on the Net but most of them are
not so efficient (even if they probably be used too).
(I like this one : http://www.alsacreations.fr/dewplayer )

But I'am seriously very curious to see *with an example* how you do to
play a sound on demand (a "clic" when clicking a link for instance)
without using Flash and without downloading the sound each time.
Because personally I can't do it.
 
S

SAM

Kevin Scholl a écrit :
Nor does the presence of Flash necessarily mean that it's turned on. A
number of my colleagues (myself included) utilize a plugin that blocks
Flash from loading automatically (replacing it with the Flash icon so
we know it's there) unless we specifically click it to activate.

You're right.
I've recently downloaded a flash blocker.
And the JS also can be disabled too.

The Net will kill the Net ... :-(
 
T

Thomas 'PointedEars' Lahn

David said:
If you are writing an Ajax chat application, then you should be able
to tackle this. As with Flash, you need two branches, one that checks
the plugins and/or mimeTypes collections to determine support for your
chosen format and a fallback for IE and its various derivations (e.g.
old AOL browsers, which have no such collections (the feature
detection pattern should be obvious.)

The first branch adds an object element to the body to play a sound
and removes it after a suitable delay (which cannot be calculated
exactly as it takes time to download the file and load the plug-in.)

I don't see a need for removing the element, especially not because of
the uncertainty you mentioned.

Audio compression aside, I also don't see a need for Flash here. User
agents are capable to load the audio resource directly with the object
element. You will have to use another branch for MSHTML, though, that
uses a CLSID to trigger the Windows Media Player ActiveX control.
The second branch adds a bgsound element (or two) to the head and
changes the src property to play sounds.

There really is no need for that, unless you want to support IE 4.


PointedEars
 
J

Joost Diepenmaat

Thomas 'PointedEars' Lahn said:
Audio compression aside, I also don't see a need for Flash here. User
agents are capable to load the audio resource directly with the object
element.

This is an unusually sweeping statement coming from you. My main browser
(firefox 2.something on debian) certainly doesn't play mp3s when they're
provided via an object tag.
 
D

David Mark

I don't see a need for removing the element, especially not because of
the uncertainty you mentioned.

You have to remove it, set the src property and re-add it to get a new
sound to play (at least in the browsers I have tested.) And then
there is the issue of fast history navigation. Leave the object in
place and the last sound played will play again when you return to the
page, which is very annoying.
Audio compression aside, I also don't see a need for Flash here.  User
agents are capable to load the audio resource directly with the object
element.  You will have to use another branch for MSHTML, though, that
uses a CLSID to trigger the Windows Media Player ActiveX control.


There really is no need for that, unless you want to support IE 4.

It works far better in IE than the object approach. It is faster and
does not require ActiveX.
 
Ad

Advertisements

S

SAM

Thomas 'PointedEars' Lahn a écrit :
I don't see a need for removing the element, especially not because of
the uncertainty you mentioned.
OK

Audio compression aside, I also don't see a need for Flash here. User
agents are capable to load the audio resource directly with the object
element.

To load, probably, but to play it ? ... less sure.
You will have to use another branch for MSHTML, though, that
uses a CLSID to trigger the Windows Media Player ActiveX control.

What I would like to know is how you reactivate (replay) the sound
embeded in the object ? (without reloading the sound's file)
with any browser.

(and how an object can play a sound : certain of my browsers need the
tag embed)
There really is no need for that, unless you want to support IE 4.

And my NC4 ? do you think to my NC4 ? (with its AudioLive)
 
D

David Mark

David Mark a écrit :


Oooops!
I thought we told of this one :
<http://www.schillmania.com/projects/soundmanager2/>
issued from :
<http://www.schillmania.com/projects/soundmanager/>
(perhaps are they all the same ?)

No, the sequel is far more bloated and appears to be even less
competent than the original:

this.getMovie = function(smID) {
// return self.isIE?window[smID]:document[smID];
return self.isIE?window[smID]:(self.isSafari?document[smID+'-
embed']:document.getElementById(smID+'-embed'));
}

Translation:

If some meaningless property set by sniffing the user agent string is
true, treat elements as properties of the window object. Otherwise,
if some other meaningless property is true, treat elements as
properties of the document object. And for everything else, assume
that the standard DOM method is available. Scary stuff.

It should be pointed out that like most Flash scripts, these browser
indicator variables are used for the most critical decision of all: to
use ActiveX or not. Needless to say, avoid this (and similar scripts)
like the plague.

[snip]
I only have a look to the code after having pushed some of proposed
buttons in soundManager_1 that did noises (yes they did !)

They did for me too (in IE7.)
And then ?
The sound has to be coded anyway (even it is a midi), no ?

If you have a library of WAV, SND, MIDI, etc. files, then having to
convert them all to MP3 is a considerable drawback.
I don't say the OP has to have its page to download an elephant if he
just needs a mouse.
I say only that soundmanager is the best (the easiest way) to serve sounds..

I say it is a monstrously bad script and should be avoided at all
costs.
There are some others Flash mp3 players on the Net but most of them are
not so efficient (even if they probably be used too).
(I like this one :http://www.alsacreations.fr/dewplayer)

Why use Flash at all?
But I'am seriously very curious to see *with an example* how you do to
play a sound on demand (a "clic" when clicking a link for instance)

I would never do such thing, but yes, it is certainly possible. I
have included an audio module in my library that allows for this sort
of user abuse. Would you like to help with the Beta testing? I would
like to get a few more Mac testers on board. Send me an email if you
have some spare time.
without using Flash and without downloading the sound each time.
Because personally I can't do it.

Audio files are cached just like other resources.
 
D

David Mark

This is an unusually sweeping statement coming from you. My main browser
(firefox 2.something on debian) certainly doesn't play mp3s when they're
provided via an object tag.

Either you do not have an appropriate plug-in installed (something
that any such script would need to detect), plug-ins are disabled or
the object markup is wrong.
 
D

David Mark

Thomas 'PointedEars' Lahn a écrit :




OK

No, Thomas is incorrect here. See my follow-up.
To load, probably, but to play it ? ... less sure.

It works. At least in the browsers and plug-ins I have tested.
What I would like to know is how you reactivate (replay) the sound
embeded in the object ? (without reloading the sound's file)
with any browser.

I explained that.
(and how an object can play a sound : certain of my browsers need the
tag embed)

Perhaps NN4. That's about it. If you are going to *nest* objects
with param elements, then you need the non-standard embed element to
support older versions of Safari. Most Flash scripts wrap an object
around an embed, whether they need to or not. Regardless, the process
I described does not nest objects.
And my NC4 ? do you think to my NC4 ? (with its AudioLive)

NN4? Perhaps you can tell me (see my other post.) I haven't tested
it in almost a decade (and when I did, it was surely with an embed
element.)
 
S

SAM

David Mark a écrit :
You have to remove it, set the src property and re-add it to get a new
sound to play (at least in the browsers I have tested.)

Yeap I think so with tag object

Something like :
var Oold = document.getElementById('mySound');
var Onew = Oold.cloneNode(true);
Onew.data = Onew.getElementsByTagName('embed')[0].src = 'newSound.mid';
Oold.parentNode.replaceChild(Onew, Oold);

It works far better in IE than the object approach. It is faster and
does not require ActiveX.

even to replay the same sound without relaoding it ?
 
Ad

Advertisements

J

Joost Diepenmaat

David Mark said:
Either you do not have an appropriate plug-in installed (something
that any such script would need to detect), plug-ins are disabled or
the object markup is wrong.

Currently I don't have a plugin installed. I used to have a plugin
installed (I think it was the VLC plugin), but it always set the volume
to 0% and wouldn't auto-start so it was pretty useless.

In any case, if you're relying on plugins anyway, my /guess/ would be
that flash has the largest install base of any plugin.

What I'm getting at is that just using <object> tags is no guarantee
whatsoever and that, currently, using flash is probably a very sane
fallback/default if you want reasonably reliable scriptable sound.
 
T

Thomas 'PointedEars' Lahn

Joost said:
This is an unusually sweeping statement coming from you. My main browser
(firefox 2.something on debian) certainly doesn't play mp3s when they're
provided via an object tag.

It then is likely that you have done something wrong. I'm not on Debian
GNU/Linux now, but it works in Firefox 2.0.0.11 on Windows and I remember
it working on Debian (must have been Firefox 1.5.x) as well.

It also works in IE 7, Opera 9.25, NN 7.2, and even NN 4.8 on Windows.

I had created a test case then, slightly adapted now:

http://PointedEars.de/scripts/test/dom/multimedia/liveconn

If that doesn't work for you (script-support enabled), you should check your
about:plugins. It could be that a) you have no plugin that can handle the
media type or b) an obsolete or misconfigured player plugin prevents the
sound from playing.


PointedEars
 
D

David Mark

David Mark a écrit :


You have to remove it, set the src property and re-add it to get a new
sound to play (at least in the browsers I have tested.)

Yeap I think so with tag object

Something like :
var Oold = document.getElementById('mySound');
var Onew = Oold.cloneNode(true);
Onew.data = Onew.getElementsByTagName('embed')[0].src = 'newSound.mid';
Oold.parentNode.replaceChild(Onew, Oold);

Not exactly, but that will work as well.
even to replay the same sound without relaoding it ?

You set the src to "", instead of removing the element.
 
Ad

Advertisements

T

Thomas 'PointedEars' Lahn

David said:
Either you do not have an appropriate plug-in installed (something
that any such script would need to detect), plug-ins are disabled or
the object markup is wrong.

I don't think that with this approach the script would need to detect
whether there was an appropriate plugin installed (is that even possible
in !MSHTML?). If there is not, simply nothing happens.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top