javascript - 从 ftp 文件夹连续音频广播

标签 javascript php audio ftp

我的网页上有一个网络广播,并且我使用不带icecast/shoutcast的服务器作为服务器从我的计算机流式传输音频。

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

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

这对我来说是个问题,因为我想要的仍然是广播心态中的广播。你知道,当你打开 radio 时,你并没有按下播放按钮。那里总是播放着一首歌......所以我决定做类似的事情:

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

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

最佳答案

到目前为止我已经到了这里...

PHP:

<? 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); ?>

HTML:

       <audio autoplay 
       controls="controls" 
       id="html5AudioPlayer" 
       src="<?php echo $initialFile ?>"></audio> //start from this file

JS:

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
 });

但仍需改进。跳跃时间的计算存在错误。帮助将不胜感激。

关于javascript - 从 ftp 文件夹连续音频广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542321/

相关文章:

javascript - React-Native:后退按钮设置保留在所有组件上

javascript - jQuery next 方法替代

javascript - 如何实现动态面包屑导航

javascript - 按键事件和真正的键盘按键有什么区别?

android - 在 Android 上播放 amr 中的音频

c - YIN-Frequency-Detection 和泛音(吉他弦)

python - python tensorflow信号处理MFCC功能

php - 数据在 Titanium 中返回为 "undefined"

php - 如何从php中的字节数组中获取图像?

javascript - 通过javascript传递数组