PHP and Javascript to play audio

Posted on 2011-10-14
Medium Priority
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 111

Expert Comment

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

Accepted Solution

re-searcher earned 2000 total points
ID: 36972009
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


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 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