PHP and Javascript to play audio

Posted on 2011-10-14
Last Modified: 2012-05-12
I have the following script that will open up a specific directory and list all of the folders and all of the files in the folders.  They are all music files.  It works great but won't play the music file.  I think it is because I'm not defining the src="" correctly.

			$("#message").text("Music started");
			$("#message").text("Music paused");
			$("#audio-player")[0].currentTime = 0;
			$("#message").text("Music Stopped");

// Set directory path
$dir = "D:music/";
  function ListFolder($path)
    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");
    //Leave only the lastest folder name
    $dirname = end(explode("/", $path));
    //display the target folder.
    echo ("<li>$dirname\n");
    echo "<ul>\n";
    while (false !== ($file = readdir($dir_handle)))
        if($file!="." && $file!="..")
            if (is_dir($path."/".$file))
                //Display a list of sub folders.
                //Display a list of files.
                echo "<li><audio id=\"audio-player\" name=\"audio-player\" src=\"$dir $file\" ></audio>
<div id=\"message\"></div>
<a id=\"play-bt\" href=\"#\">Play music</a> | <a id=\"pause-bt\" href=\"#\">Pause music</a> | <a id=\"stop-b\t\" href=\"#\">Stop music</a></li>";
    echo "</ul>\n";
    echo "</li>\n";
    //closing the directory

  <ul class="dmxtree" id="FolderView">
<?php ListFolder($dir); ?>


Open in new window

Question by:rgranlund
    LVL 107

    Expert Comment

    by:Ray Paseur
    Is this on a server somewhere so we can see it in action?  I'd like to see the generated HTML.
    LVL 3

    Accepted Solution

    use following code for each file:
        function playsound(soundobj) {
          var thissound=document.getElementById(soundobj);
        <embed src="success.wav" autostart=false width=1 height=1 id="sound1"
    <input type="button" value="Play Sound" onClick="playsound('sound1')">

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now