Html5音频播放器可以播放超过一首歌曲

时间:2013-11-28 02:19:29

标签: javascript php html5 audio

我正在使用php glob()来获取文件夹中的歌曲列表 然后使用shuffle()来改组歌曲 然后使用foreach循环遍历歌曲。 但是我只能唱歌。我正试图让它发挥 当一首歌结束时,一首歌自动开始另一首歌 我做了很多谷歌搜索,但似乎找不到任何事情这是唯一可能的 用冰球?所有发布我的代码

          <!DOCTYPE HTML>
          <html>


          <head>
          <title>ymp</title>

         <link rel="stylesheet" href="style.css" type="text/css" />

         </head>
          <body>
          <h1>YMP RADIO</h1>
          <?php require ("dropmenu.html"); ?>




           <?php     

            $music = glob("songs/*");  // get all files in the music directory
            shuffle($music);
            foreach ($music as $song){           // try and loop a random songs

            print <<<WTF

            <audio id="player" src="$song" type="audio/mpeg"></audio>
            <audio id="player" src="$song" type="audio/ogg"></audio>

             WTF;

             }
             var_dump($song);
             ?>

             <div> 
             <table border="1px" >
             <tr>

             <td class="tdpage" >
             <img src="ymplogo5.jpg" alt=""  />
         <p><button  class="botton";   onclick="document.getElementById('player').play()">Play</button>
         <button     class="botton";   onclick="document.getElementById('player').pause()">Pause</button>
        <button     class="botton";   onclick="document.getElementById('player').volume+=0.1">Volume up </button>
       <button     class="botton";   onclick="document.getElementById('player').volume-=0.1">Volume down</button>

    </td>
    </tr>
    </table>    
    </div> 

    <br /><br /><br />
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

嗯,所以如果我理解你的问题,你似乎想要通过服务器端处理(php)实现用户交互功能(播放列表)。不幸的是,这不起作用。你的php只提供歌曲的网址。其余的工作在于客户端工作,如js。你可以做到以下几点(但是让我警告你,这并不像我认为的那样简单)

  • 从php获取url列表并在脚本标记中回显它并将其存储在js变量中。
  • 然后使用js播放第一首歌曲(请记住我们只会使用一个音频标签)
  • 然后听一个事件监听器,告诉js什么时候歌曲结束,当它结束时,播放下一首歌曲(用新歌曲替换url)。

你的大多数工作都是在js而不是php。即使你这样做,这也只能成为一个简单的媒体播放器。