473,466 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Playing a sound file with a JavaScript function

Death Slaught
1,137 Top Contributor
This code was written by acoder serveral (139) days ago. So I decided to post it.

Expand|Select|Wrap|Line Numbers
  1.  
  2. function play() {
  3. embed = document.createElement("embed");
  4. embed.setAttribute("src", "soundfile.wav");
  5. embed.setAttribute("hidden", true);
  6. embed.setAttribute("autostart", true);
  7. document.body.appendChild(embed);
  8. }
  9.  
  10.  
^_^ Thanks, Death
Feb 6 '08 #1
3 18570
gits
5,390 Recognized Expert Moderator Expert
to make it a little bit more useful we should slightly adapt it to accept the file to play as a parameter, and strictly seen we should declare the variable embed :)

Expand|Select|Wrap|Line Numbers
  1.  
  2. function play(file) {
  3.     var embed = document.createElement("embed");
  4.  
  5.     embed.setAttribute('src', file);
  6.     embed.setAttribute('hidden', true);
  7.     embed.setAttribute('autostart', true);
  8.  
  9.     document.body.appendChild(embed);
  10. }
  11.  
so now we could simply call:

Expand|Select|Wrap|Line Numbers
  1. onclick="play('file.wav');"
on any element we want ...

kind regards
Feb 7 '08 #2
acoder
16,027 Recognized Expert Moderator MVP
I probably posted that from somewhere - no need to attribute it to me.

Besides the points made above, embed is actually non-standard, but sometimes required for backwards compatibility. The element to use now is object.
Feb 7 '08 #3
gits
5,390 Recognized Expert Moderator Expert
I probably posted that from somewhere - no need to attribute it to me.

Besides the points made above, embed is actually non-standard, but sometimes required for backwards compatibility. The element to use now is object.
so we could adapt it further :)

Expand|Select|Wrap|Line Numbers
  1.  
  2. function play(file, obj) {
  3.     if (typeof obj == 'undefined') {
  4.         obj = 'object';
  5.     }
  6.  
  7.     var node = document.createElement(obj);
  8.  
  9.     node.setAttribute('src', file);
  10.     node.setAttribute('hidden', true);
  11.     node.setAttribute('autostart', true);
  12.  
  13.     document.body.appendChild(node);
  14. }
  15.  
now we could call:

Expand|Select|Wrap|Line Numbers
  1. onclick="play('file.wav');"
that creates the 'object' as default or:

Expand|Select|Wrap|Line Numbers
  1. onclick="play('file.wav', 'object');"
or even with 'embed' instead of 'object' in case we need to do that ;)

kind regards
Feb 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Paul Moore | last post by:
Does anyone know of a Python library which handles sound file formats (MP3, Ogg, FLAC are the three I'm nost interested in). I'd like something that can manipulate "metadata" - stuff like artist,...
3
by: Kim Forbes | last post by:
Hello all, I have simple function that I call from an alert box alert(testScore); It works fine. Unfortunately, I do not like the sound and Exclamation point icon that appears. I'd like a...
1
by: Peter Schmitz | last post by:
Hi, I'm just trying to play a *.wav file, making use of the unmanaged function "sndPlaySoundA" in winmm.dll, but I still got one question: How can I avoid, that the sound file itself has to be...
2
by: Serdge Kooleman | last post by:
hi i'm trying to play sounds in my windows app. wav files are in the same folder as .exe if i'm getting path by command: ApplicationPath = Application.StartupPath; and it is look like
1
by: Lam | last post by:
how can I play sound file in a .aspx page written in C#? I try to use the code like the following. But whenI call the play function play("sound.wav", this.SND_ASYNC) my computer give out "be"...
3
by: Xero | last post by:
Is there a simpler way to play from a sound file than this one http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbcode/html/vbtskcodeexampleplayingsound.asp Thanks. -- Xero ...
4
by: Benzerari | last post by:
Hi All; Please, if any one knows the visual basic code, to be added to a Stop button in order to stop playing some sound of (*.Wav) type. I have achieved to find the code for start playing sound...
26
by: Jake Barnes | last post by:
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...
1
by: Sheena777 | last post by:
I want to Play a specific sound file in my code when a certain event happens, but i don't know how. I can play the system sounds in my program and I know how to start the media player to play an mp3...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.