473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

Playing a sound file with a JavaScript function

Death Slaught
1,137 1GB
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 18561
gits
5,390 Expert Mod 4TB
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 Expert Mod 8TB
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 Expert Mod 4TB
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.