474,044 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ 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 18606
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
3545
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, title, album tags. I'm not critically interested in format conversion, or playing sound files - I can handle that with other applications. I'm using Windows, and Python 2.3, if it matters (as it might for C extensions). I can manage with just...
3
21502
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 nice message box like VBA message box #64 to appear. I know hardly anything at all about VB or VBA. But, I do know you can call a VB function from Javascript. How? Is it
1
2897
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 seperate file in a specific directory, but instead 'integrate' it in my application? Thanks peter
2
1876
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
8028
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" sound instead playing the file (same code works fine in a windows application written in c# , that mean there is nothing wrong with my sound card.) thanks ----------------------------------------------------------------------------...
3
1215
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 http://www.chezjeff.net My personal web portal
4
5090
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 or music which is: OLE.DoVerb (vbOLEPrimary) But the one to stop or interupt the sound I could not find it. Please let know
26
3949
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 almost no posts from the last 3 years: http://groups.google.com/group/comp.lang.javascript/search?group=comp.lang.javascript&q=how+to+play+a+sound+with+Javascript&qt_g=Search+this+group Even after sorting by date, there don't appear any...
1
2536
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 but I want to be able to play the sound file without an external player. Is there any other way of playing a sound file in vb.net code when an event is fired?
0
10546
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11602
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12022
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11141
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8698
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7868
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.