如何使用单个php文件和多个媒体文件创建播放列表


How to create a playlist with a single php file and multiple media files?

我正试图创建一个很棒的音乐播放列表,只使用一个siglephp文件。

现在,有了这个代码,我可以从一个媒体文件流传输一首歌来源:

<?php
  // Try and open the remote stream
  if (!$stream = fopen('http://example.com/audio.mp3', 'r')) {
    // If opening failed, inform the client we have no content
    header('HTTP/1.1 500 Internal Server Error');
    exit('Unable to open remote stream');
  }
  // It's probably an idea to remove the execution time limit - on Windows hosts
  // this could result in the audio stream cutting off mid-flow
  set_time_limit(0);
  // Inform the client we will be sending it some MPEG audio
  header('Content-Type: audio/mpeg');
  // Send the data
  fpassthru($stream);
  // Thanks DaveRandom
?>

因此,这个想法几乎很简单:使用一个php文件,创建一个播放列表。php文件流式传输第一首歌曲,然后在中间播放另一首歌曲(之前在php文件中设置),并一遍又一遍地播放,所有源代码都位于php文件中。

谢谢。

流媒体是实时协议。php使用单一http请求响应协议。这意味着,播放器不会自动调用php来播放另一首歌曲。在最好的情况下,玩家会在曲目结束时停止,只有当listner再次点击播放时,才会发送第二个请求。

现在,您需要有办法识别第二个播放另一首曲目的请求。会话可能在非基于浏览器的用户代理(如媒体播放器)中不起作用。因此,您可以在php查询字符串中拥有唯一的id,并拥有一个本地db/etc来跟踪上次播放的内容。

无论哪种方式,曲目的自动切换都需要更多的php代码、http缓冲区检查以及mp3文件解析,以便在没有下一个mp3文件头/id3/的情况下解析最后一个流并与下一个流连接。

正如@nomaD所说,你必须使用客户端脚本,计算曲目的分钟数,然后运行计时器,一旦达到时间,就用新曲目调用另一个php。你可能需要检查一下http://www.schillmania.com/projects/soundmanager2/demo/api/