Click image to start and stop music

Discussion in 'JavaScript' started by annbee, Oct 19, 2010.

  1. #1
    If anyone knows how to do this..please let me know..thanks..javascript or swf is fine too.
     
    annbee, Oct 19, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    I think this example will help you

    
    <html>
    
    <head>
      <title>Play and stop music</title>
      <script type="text/javascript">
          function playmusic(){
              var embed = document.createElement("embed");
    
              embed.setAttribute("id", "music");
              embed.setAttribute("src", "music.mp3");
              embed.setAttribute("hidden", true);
              embed.setAttribute("autostart", true);
    
              document.body.appendChild(embed);
    
              document.getElementById("image").setAttribute("onclick","stopmusic();");
          }
    
          function stopmusic(){
              document.body.removeChild(document.getElementById("music"));
    
              document.getElementById("image").setAttribute("onclick","playmusic();");
          }
        </script>
    </head>
    
    <body>
    
    <img id="image" src="image.jpg" onclick="playmusic();">
    
    </body>
    
    </html>
    
    Code (JavaScript):
     
    s_ruben, Oct 19, 2010 IP
  3. annbee

    annbee Member

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    oh wow thank you but the stop doesnt seem to work..the music just keeps playing
     
    Last edited: Oct 20, 2010
    annbee, Oct 20, 2010 IP