一个简单的PHP计时器

时间:2015-06-06 19:52:27

标签: php timer mp3

我目前有一个允许音频上传的网站,您可以通过这些上传来制作播放列表。上传时,我有get3()来获取持续时间并将其保存在数据库中。

目前我有文件自动播放,但是,数据库中持续时间的目的是制作一个定时器,该定时器将采用该数字,并在歌曲播放时倒计时。当它达到数字'持续时间'在数据库中,我希望它然后为流媒体目的加载一首新歌。最好的方法是什么?

有关其他信息,有没有办法获得当前时间,如果他们将滑块移动到歌曲的中间,以便计时器可以赶上?或者,有没有更好的方法来做到这一点,甚至没有使用计时器?

2 个答案:

答案 0 :(得分:0)

您可以使用计时器类

<?php

class Timer {

   var $classname = "Timer";
   var $start     = 0;
   var $stop      = 0;
   var $elapsed   = 0;

   # Constructor
   function Timer( $start = true ) {
      if ( $start )
         $this->start();
   }

   # Start counting time
   function start() {
      $this->start = $this->_gettime();
   }

   # Stop counting time
   function stop() {
      $this->stop    = $this->_gettime();
      $this->elapsed = $this->_compute();
   }

   # Get Elapsed Time
   function elapsed() {
      if ( !$elapsed )
         $this->stop();

      return $this->elapsed;
   }

   # Resets Timer so it can be used again
   function reset() {
      $this->start   = 0;
      $this->stop    = 0;
      $this->elapsed = 0;
   }

   #### PRIVATE METHODS ####

   # Get Current Time
   function _gettime() {
      $mtime = microtime();
      $mtime = explode( " ", $mtime );
      return $mtime[1] + $mtime[0];
   }

   # Compute elapsed time
   function _compute() {
      return $this->stop - $this->start;
   }
}

?>

或者您可以查看计时器类here

的文档

答案 1 :(得分:0)

通过使用html 5音频标签,您可以让JavaScript确定歌曲的持续时间,然后使用几行代码播放下一首歌曲。

在这个示例中,我使用的是tracks变量,您可以使用php json_encode()轻松构建。

&#13;
&#13;
var player;
var playlist;
var tracks;
var current;

$(function() {
  current = -1;
  player = $('#audio')[0];
  playlist = $('#playlist');
  tracks = [{
      title: "The Kyoto : Hachiko (The Faithtful Dog)",
      src: "https://freemusicarchive.org/music/download/34342f0461bd14291ab21bae5f1bfc57b3120677.mp3"
    },
    {
      title: "Jahzzar  : The last ones",
      src: "https://freemusicarchive.org/music/download/8d97152c47959e384ed4b991ac8012665e121eac.mp3"
    },
    {
      title: "Gillicuddy  : Jupiter The Blue",
      src: "https://freemusicarchive.org/music/download/bf36de47dafa5c162b45a85a90739f3ef1160832.mp3"
    }
  ];

  function next() {
    current++;
    if (current > tracks.length) {
      current = 0;
    }

    player.src = tracks[current].src;
    player.load();
    player.play();
  }

  // Set volume down a bit and play on page load.
  player.volume - .10;
  next();

  // Event that fires when the song ended.
  player.addEventListener('ended', function(e) {
    next();
  });

  // Write playlist in document.
  for (let o of tracks) {
    playlist.append('<li><a href="' + o.src + '">' + o.title + '</a></li>');
  }
});
&#13;
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>

<audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
  <source type="audio/mp3" src="">
  Sorry, your browser does not support HTML5 audio.
</audio>
<ul id="playlist"></ul>
&#13;
&#13;
&#13;