Playing sound using javascript

I want to create a table with a row and a button (which plays a sound) in each row, and each button plays a different sound.

I want to do this with this method:

<script> function EvalSound(soundobj) { var thissound=document.getElementById(soundobj); thissound.Play(); } </script> <embed src="success.wav" autostart=false width=1 height=1 id="sound1" enablejavascript="true"> 

and this is the button:

 <form> <input type="button" value="Play Sound" onClick="EvalSound('sound1')"> </form> 

The problem is what I want here:

 <input type="button" value="Play Sound" onClick="EvalSound('sound1')"> 

to write the url of the file instead of "sound1", can I do it this way? or do I need to change another code in the code?

Edit:

I will build the script as follows:

 <script> function EvalSound(soundobj) { var embed = document.createElement('embed'); embed.setAttribute('width',1); embed.setAttribute('height',1); embed.setAttribute('src',soundobj); embed.setAttribute('autostart', false); embed.setAttribute('enablejavascript', true); embed.Play(); } </script> 

and name it with:

 <form> <input type="button" value="Play Sound" onClick="EvalSound('sound url')"> </form> 

and it still does not reproduce sound.

+4
source share
3 answers

You can achieve this as follows:

refer to the blog How to play sound on a click or on MouseOver

 <script language="javascript" type="text/javascript"> function playSound(soundfile) { document.getElementById("dummy").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"; } </script> 

On the web page

 <span id="dummy"></span> <a href="#" onclick="playSound('URL to soundfile');">Click here to hear a sound</a> <p onmouseover="playSound('URL to soundfile');">Mouse over this text to hear a sound</p> 
+5
source

NOTE. This is not HTML5. You can get the source using thissound.src . You can then add it to the DOM anywhere you want to place it.

+1
source

You can use the javascript framework to make your code easier. Take a look at the Scriptaculous page:

http://madrobby.github.com/scriptaculous/sound/

As far as I know, "jQuery" is not so well assigned to sound, however, I think there are plugins for that.

Then you can put your audio files as links or "rel" links, have buttons on the buttons, and play them with fairly clean HTML and a few scripts.

0
source

Source: https://habr.com/ru/post/1393955/


All Articles