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

发布于 2025-01-04 09:38:12 字数 835 浏览 1 评论 0原文

好吧,我正在尝试创建一个很棒的音乐播放列表,仅使用一个 php 文件。

现在,使用这段代码,我可以从单个媒体文件源流式传输一首歌曲:

<?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 文件中。

谢谢。

well I'm trying to create a great playlist of music, only using a sigle php file.

Right now, with this code I can stream a song from a single media file source:

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

?>

So, the idea is almost simple: with a single php file, create a playlist. The php file streams the first song, and then inmediatly, play another one - previously set on the php file - and over and over again, with all of the sources in the php file.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2025-01-11 09:38:12

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

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

不管怎样,自动切换曲目将需要更多的 php 代码、http 缓冲区检查以及 mp3 文件解析,以便最后一个流并附加到下一个流,而无需下一个 mp3 文件头/id3/等。

Streaming is realtime protocol. While php uses single http request-response protocol.
Which means, your php won't be called automatically to play another song by the player automatically. At best, the player will stop at end of track and only when listner clicks play again, the second request will be sent.

Now you need to have way to identify second request to play another track. sessions probably don't work in non-browser based user-agents (like media players). so you can have unique id in your php query string and have a local db/etc to keep track of what was played last time.

Either way, automatic switching of tracks would need much more php code, http buffer checking and also mp3 file parsing so the last stream and attach with next stream without the next mp3 file headers/id3/etc.

赠我空喜 2025-01-11 09:38:12

正如 @nomaD 所说,您必须使用客户端脚本,并计算轨道的分钟数,然后运行计时器,一旦到达时间,只需使用新轨道调用另一个 php 即可。您可能需要检查此 http://www.schillmania.com/projects/soundmanager2/演示/api/

As @nomaD said you have to use a client side script, and calculate numbers of minutes for the track, then run a timer and once it reaches the time just call another php with a new track. You might need to check this http://www.schillmania.com/projects/soundmanager2/demo/api/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文