JavaScript play a Sound

Hi, this is driving me nuts and have tried several approaches.

Why when I call a JS fucntion with this line in it does it not play a sound?

document.getElementById("music").src = "keystroke.mp3";

The mp3 file exsists in the same folder as the JS script and I can prove it is audible with the Microsoft Media Player. Can someone please put me out of my misery? Thanks.
LVL 1
edhastedAsked:
Who is Participating?
 
kswathiCommented:
can you tell me which player are you using. You need to start the player to play the music. Also check the file path. Hope it helps
0
 
edhastedAuthor Commented:
I use relative pathing throughtout this web-site so am very confident about the files location, although did put the offending MP3 in all principal locations out of paranoia just before I posted.

http://www.phon.ucl.ac.uk/home/mark/audio/play.htm
> I have tried a similar approach on Saturday without any joy. Will try this approach tomorrow night.

http://www.javascriptkit.com/script/script2/soundlink.shtml 
> Yes I tired this one last night, again without any joy.

Don't get the poiint about the media player beign ready to play - am I missing somehting?
If I play a sound from the computer it works...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
edhastedAuthor Commented:
http://www.phon.ucl.ac.uk/home/mark/audio/play.htm

Have been able to get this approach to work, many thanks. Will run some more tests tomorrow night and report back.
0
 
edhastedAuthor Commented:
From the UCL site I used option 4. Controlling an Embedded Sound using JavaScript (New)

It works on IE9 but not on Opera 11.51 or FireFox 5.0.

Any ideas on how to make such a fundametal task universal?
0
 
Gurvinder Pal SinghCommented:
please share the code that you have tried
0
 
edhastedAuthor Commented:
The PHP call is

onkeyup="EvalSound('sound1')";

The JavaScript reads

<script>
function EvalSound(soundobj) {
 var thissound= eval("document."+soundobj);
 try {
     thissound.Play();
 }
 catch (e) {
     thissound.DoPlay();
 }
}
</script>

As I say IE works but Firefox and Opera appear not to...
0
 
edhastedAuthor Commented:
Any ideas - this works for IE but not Firefox or Opera. There must be a universal solution for such a basic task?
0
 
Gurvinder Pal SinghCommented:
It seems that there is no way to get control over embed tag using javascript.

Try using video tag
http://www.w3schools.com/html5/tag_video.asp
http://www.quackit.com/html_5/tags/html_video_tag.cfm
0
 
edhastedAuthor Commented:
This appears to be the best solution that I have found.
Unfortunately such a simple actions seems to be hard to find a universal solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.