dcsimg
www.webdeveloper.com
Results 1 to 10 of 10

Thread: PROBLEM: play all mp3 files from dir. PHP

  1. #1
    Join Date
    Nov 2015
    Posts
    6

    PROBLEM: play all mp3 files from dir. PHP

    Hi I've a folder with some mp3 files. I tried to use this code for play all files from my directory but audio tag play only one and just stop. Any idea?

    Thanks.

    <?php
    $files = glob("audioupload/*.mp3");
    $random = $files[array_rand($files)];
    ?>

    <body>
    <audio src="<?php echo $random; ?>" controls></audio>
    <body>

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    541
    That's quite the behavior one would expect because you only pulled one out of all the files found and put it in the src attribute of the audio element. Tried refreshing the browser to see if a different file is played next. If that works then you know your back-end script is OK.
    You might need JavaScript on the front-end to feed the next file to the audio element once the playback is done.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,923
    I think you'll need some JavaScript. For instance, as one example http://devblog.lastrose.com/html5-audio-video-playlist/ shows some browser-side code for creating a HTML5 audio object with a playlist. In your PHP, you could loop through your files array to create the HTML used to list each file, which the JavaScript then accesses to play those files. (You could use PHP's shuffle() function to randomize the order.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Join Date
    Nov 2015
    Posts
    6
    ...thank you members...yours advises were pretty good....Yes if I reload the page, the player change file audio so now I've tried with js and i think it's the good way. The problem is NOT SOLVED but now repeat always the same mp3 it's something new. Here the new code if interesting. Thanks.

    <?php
    $files = glob("audioupload/*.mp3");
    $random = $files[array_rand($files)];
    ?>
    <audio id="myRadio" style="display:none" src="<?php echo $random; ?>"></audio>
    <script type="text/javascript">
    document.getElementById('myRadio').addEventListener("ended",function() {
    this.src = "<?php echo $random; ?>?nocache="+new Audio('<?php echo $random; ?>'); <--- i think error is here!
    this.play();
    });
    </script>

  5. #5
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    541
    <audio id="myRadio" style="display:none" src="<?php echo $random; ?>"></audio>
    <?php
    $listStr='';
    foreach($files as $file){
    $listStr.=($listStr!==")?", ":"";
    $listStr.="'$file'";
    }

    ?>
    <script type="text/javascript">
    var playListArray=Array(<?php echo $listStr;?>); //array of files in the playlist
    var audioPlayer=document.getElementById('myRadio'); //audio player HTML element

    //Sets the next audio file to be played
    function playNextSound(){
    //random index from the playList array
    var fileIndex=Math.floor(Math.random()*playListArray.length);

    //set the audio source file using the random index
    audioPlayer.src=playListArray[fileIndex];
    }

    audioPlayer.addEventListener("ended", playNextSound, false); //listens to the end of the playback
    </script>

  6. #6
    Join Date
    Nov 2015
    Posts
    6
    Thanks I tried to put your code but page show an error it's possible i ve to combine the php like this?

    <?php
    $files = glob("audioupload/*.mp3");
    $listStr='';
    foreach($files as $file){
    $listStr.=($listStr!==")?", ":""; <---- here i think there s something wrong
    $listStr.="'$file'";
    }
    $random = $files[array_rand($files)];
    ?>

  7. #7
    Join Date
    Nov 2015
    Posts
    6
    The Code works wonderful you've SOLVED my problem....thank you holyhttp... ...down here complete code for helping future members.


    <?php
    $files = glob("DIRECTORY/*.mp3");
    $random = $files[array_rand($files)];
    ?>
    <audio id="myRadio" style="display:none;" src="<?php echo $random; ?>"></audio>
    <?php
    $listStr='';
    foreach($files as $file){
    $listStr.=($listStr!=='')?", ":"";
    $listStr.="'$file'";
    }

    ?>
    <script type="text/javascript">
    var playListArray=Array(<?php echo $listStr;?>); //array of files in the playlist
    var audioPlayer=document.getElementById('myRadio'); //audio player HTML element

    //Sets the next audio file to be played
    function playNextSound(){
    //random index from the playList array
    var fileIndex=Math.floor(Math.random()*playListArray.length);

    //set the audio source file using the random index
    audioPlayer.src=playListArray[fileIndex];
    }

    audioPlayer.addEventListener("ended", playNextSound, false); //listens to the end of the playback
    </script>

  8. #8
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    541
    I am glad it all works well. It's my pleasure to give a hand to a fellow web developer.

  9. #9
    Join Date
    Jun 2016
    Posts
    1
    hi..I am working a project to play audio files on a web browser with the help of a specified path of the directory..I have tried executing the above code but it shows an error called undefined index...I am trying to run this code on my browser localhost using xampp..Any help will be appreciated..Thanks in advance

  10. #10
    Join Date
    Jul 2016
    Location
    India
    Posts
    3
    If you are getting warning says "undefined index" that means that array index is not defined which you used in code.


    Quote Originally Posted by newtophp View Post
    hi..I am working a project to play audio files on a web browser with the help of a specified path of the directory..I have tried executing the above code but it shows an error called undefined index...I am trying to run this code on my browser localhost using xampp..Any help will be appreciated..Thanks in advance

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles