Javascript 从ftp文件夹进行连续音频广播

Javascript 从ftp文件夹进行连续音频广播,javascript,php,audio,ftp,Javascript,Php,Audio,Ftp,我的网页上有一个互联网收音机,我正在使用不带icecast/shoutcast的服务器从我的计算机上传输音频 但我最近没空做这件事,我决定上传一些mp3到我网站的ftp文件夹。 我可以通过php检索mp3文件夹文件,将它们解析为js,并使用.oneded方法在html5音频元素中播放下一首歌曲 问题是(实际上这不是问题,这是自然行为)每当访问者点击播放按钮时,播放器开始播放js播放列表阵列中的第一个文件,并跳到下一个文件,直到整个阵列完成 这对我来说是个问题,因为我想要的仍然是一个电台的广播。你

我的网页上有一个互联网收音机,我正在使用不带icecast/shoutcast的服务器从我的计算机上传输音频

但我最近没空做这件事,我决定上传一些mp3到我网站的ftp文件夹。 我可以通过php检索mp3文件夹文件,将它们解析为js,并使用.oneded方法在html5音频元素中播放下一首歌曲

问题是(实际上这不是问题,这是自然行为)每当访问者点击播放按钮时,播放器开始播放js播放列表阵列中的第一个文件,并跳到下一个文件,直到整个阵列完成

这对我来说是个问题,因为我想要的仍然是一个电台的广播。你知道,当你打开收音机时,你没有按播放按钮。有一首歌一直在那里播放。。。所以我决定这样做:

我发现一些php代码通过比较mp3的大小和比特率来计算mp3的持续时间。我的计划是计算所有mp3的总持续时间,然后检查第一个文件的上传时间并从现在开始减去()以找到播放时间,然后计算出播放多少mp3和播放多少秒的时间,最后使音频元素从mp3的那一点继续

这可能吗?一些代码建议?有更好的主意吗?

我来了这么远

PHP:


HTML:


洗牌列表和自动播放就足够了吗?甚至不需要洗牌。我将按播放顺序准备歌曲。我认为这也会使估算计算更容易…顺便说一句,好的MP3使用VBR,这意味着你不能从文件大小和比特率中区分持续时间,除非它使用ABR,这是比较少见的。CBR支持零位静默,所以不要期望从php播放高质量的光盘…是的,实际上我已经改变了。好的,一个想法是让每个播放列表会话的服务器端cookie跟踪所有这些数据。我的意思是,创建一次,并摆脱所有这些繁重的服务器端任务,尤其是当文件数量达到数百个时。
<? php
$ii = 0; //some counter also needed in javascript array
$totalDuration = 0; //total duration of mp3s need to be passed
$str=' '; //some string needed for javascript array
$handleAudioMp3 = 'audio/mp3/'; //my mp3 directory. all mp3s encoded carefully in CBR
$fileMp3 = scandir($handleAudioMp3); //get the items alphabetically
  foreach ($fileMp3 as &$value) {
    if ((!in_array($value,array(".","..","...")))){ //if its not parent,etc
      $f = $handleAudioMp3.$value; //let's define our file
      if ($ii==0) { //if it's the first file
        $eplased = (time()-filemtime($f)); //time eplased until the creation of first file
      }
      $m = new mp3file($f); //mp3file class http://www.zedwood.com/article/php-calculate-duration-of-mp3
      $a = $m->get_metadata(); //get the meta data of file
      if (($totalDuration + $a[Length])<$eplased) { //if the total duration passed mp3s still below the eplased time
        $totalDuration += $a[Length]; //add the file's length attr to total passed duration
        $initialFile=$f; //initial file will be the last file below eplased time
        unset($a); //release $a
      } 
      else { //if total duration exceeds eplased time then build js playlist array
        $str=$str.'audioPlayList['.$ii.'] = "'.$f.'";'; //build string for js
        $ii++;
      }
      }
  }
//WHY?
$sec=($eplased-$totalDuration); //problematic line. it should be the time added to initial file but sometimes it's very inaccurate, even negavite... 
unset($f);
unset($m);
unset($ii);
unset($value);
unset($eplased);
unset($totalDuration);
closedir($handleAudioMp3); ?>
       <audio autoplay 
       controls="controls" 
       id="html5AudioPlayer" 
       src="<?php echo $initialFile ?>"></audio> //start from this file
var audioPlayList = new Array();
<?php echo $str ?> //built array

//find the audio section
var html5Audio = document.getElementById('html5AudioPlayer');

var i=0;
//attach event - on end - move to next one
 html5Audio.onended = function(){
 html5Audio.src = audioPlayList[i];
 i+=1
}
var ii = <?php echo $sec ?>; //seconds to continue
//attach event - play - to notify others
html5Audio.addEventListener('play', function () {
this.currentTime = ii;
ii=0; //play all files from 0 after all
 });