Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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.
0
edhasted
Asked:
edhasted
  • 6
  • 3
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now