4

i have asked this question in a couple of places, but have yet to find or receive an answer. i am a code newbie, so any help would be greatly appreciated.

i am using in-page javascript on some of my pages to play a sound on clicking an image which loops a backing track to practise scales / lead guitar to.

here's the script:

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

and here's how i'm calling it for each image / mp3:

<p><a title="Play file" href="#" onclick="playSound('mp3 url');"><img title="Play" src="play button url" alt="Play" width="48" height="48" /></a>  Blues in <strong>C</strong> : backing for lead melody. Chords: C,F, G</p>

my first question - the track isn't looping - is there something wrong with the script?

my second question - how do i extend the function so that on clicking the png to play the file it is swapped for another - stop button - which stops the loop when clicked.

many thanks, jan

DaveR
  • 2,355
  • 1
  • 19
  • 36
janimal
  • 41
  • 1
  • 2

1 Answers1

2

Welcome to Stack Overflow.

Embedding sounds into web pages is a really tricky business and getting a solution that will work across all browsers and on mobile devices is a real challenge.

Fortunately there are libraries that you can use to make the embedding, playing, looping, switching of mp3s much, much easier.

I'd recommend looking into Soundmanager2, before you get too much further into this project to see if it could save you a lot of coding time, effort and frustration!

DaveR
  • 2,355
  • 1
  • 19
  • 36
  • hi. thank you very much for the prompt response - i've had this question festering on a number of forums for several weeks and had no responses. do you know whether soundmanager will allow me to keep my styling - see http://dev.budestrings.co.uk/grade-3/ because i've worked really hard to get this looking right. many thanks, jan – janimal Aug 19 '12 at 11:08
  • Yes, Soundmanager just takes care of the business of playing sounds - the look and feel of your page is up to you. – DaveR Aug 19 '12 at 11:59
  • Which JavaScript files will I need to include on a web page in order to use SoundManager2? – Anderson Green Feb 23 '13 at 03:31