douzhi7070 2017-01-12 12:26
浏览 45

如何让网站上的每个访问者都在音频文件的同一点来模拟预定的直播流?

I am attempting to simulate a scheduled live audio stream without the use of any third party tools/software. In order to do so, I would need every visitor on the website to be on the same point on the audio file. My initial plan was to have a PHP script that keeps track of the time, and write to a .json file :

ini_set('max_execution_time', 0);
include 'mp3file.class.php';
$file = "./audioDuration.json";

$mp3file = new MP3File("Nightride.mp3");
$duration = $mp3file->getDurationEstimate();

$tracker = 0;
while($tracker < $duration){
    $tracker++;
    file_put_contents($file, $tracker);
    sleep(1);
}

And the Javascript :

$.getJSON( "audioDuration.json",
    function( returnedData ) {
        document.getElementById('audioElement').currentTime = returnedData;
}

However, being completely new to PHP, I did not realize that any user can run this script on their own browser, and it would cause the audioDuration.json to contain the wrong data. I've done some research, and it appears that there are ways to have a PHP script only run if the server requests it. I am not sure if this is the most practical way to accomplish this.

  • 写回答

1条回答 默认 最新

  • dougudu3564 2017-01-12 12:45
    关注

    I feel you should use a server side resource to be sure any client get the same "time" to set-up your audio file.

    Why don't you use something like server date('H:i:s); function. If you get a 1hour long file you just need to dont take care about hours, and use only minutes and seconds to get which time should be used to start the audio file.

    And you don't even need to use javascript to call server to get the value. If you use php to generate your HTML you can directly print value in the HTML's javascript when loading the page, something like :

    echo '<script type="text/javascript">
        document.getElementById('audioElement').currentTime = ' . $timer . ';
    </script>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程