SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Threaded View

  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Playing Sound with Javascript

    Hey, guys.

    Alright, I have to admit, I'm entirely new to javascript, so, uhh... be nice?

    Anyway: What I'm trying to do is write myself a script that will play an embedded mp3 (a single file, not a playlist), but only when a link element is clicked on. As in: I don't want the file itself to be visible on the page, I don't want to have to redirect people to another page or to a pop-up window, to be able to do this. I'd rather not have to rely on browsers plugins or activeX controls that people may or may not have installed, if that's possible...

    Which I'm not sure it is. One or two blog/forum posts I've seen suggest that it IS possible to get such a script working cross-browser, but those people either didn't provide examples of the script they were working on, or linked to sites that no longer work.

    I've seen some things that say that the javascript Play method isn't supported by Firefox, but that information was about three years old, so I've no idea if it's still true. Other sites seem to use the Play method successfully (though the one I saw was using embedded flash movies, not mp3s) on firefox, and other sites still suggest that it's not the Play method that's not supported by javascript, but the getElementById or getElementbyName method.

    Anyway, yeah, I'm getting kind of lost in all this (mis?)information. Thoughts or suggestions would be hugely appreciated. Sorry for the lengthy post - but to make it lengthier still, here's the code I'm using at the moment - works in IE but not Firefox (and I have no idea if it works in any other browser).

    HTML:
    Code:
    <html>
    <head>
    <script type="text/javascript" src="player.js"></script>
    </head>
    <body>
    <embed src="audio/StraightandNarrow.mp3" autostart=false hidden=true id="straightandnarrow" name="straightandnarrow">
    <a href="#" onClick="playSong()" onDblClick="stopSong()">Straight and Narrow</a>
    </body>
    </html>
    Javascript:
    Code:
    function playSong() 
    {  
      document.getElementById('straightandnarrow').Play();
    }
    function stopSong()
    {
       document.getElementById('straightandnarrow').Stop();
    }
    (Yes, I'm aware that the embed tag is deprecated. I'm using it for two reasons: the only examples I've seen of the object tag have been for flash movies and I can't seem to get that working with mp3 files, and from what I've read it seems like the embed tag is more widely supported anyways.)

    Anyway - I'll shut up now. Thanks much.
    Last edited by Raethe; May 23, 2008 at 16:52. Reason: turned off email alert


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •