如何在请求播放嵌入的声音剪辑之前阻止将其下载到浏览器缓存?

发布于 2024-11-18 02:41:58 字数 1582 浏览 6 评论 0原文

这几天我一直在为这个问题摸不着头脑,

我正在为一个与呼叫跟踪相关的客户构建一个网络应用程序。该系统的一方面是允许直接从消息详细信息旁边的“播放”按钮播放简短的录制语音邮件消息。

我正在访问声音文件;

$wavefile=$calls_row['recording'];
if (file_exists("/var/spool/asterisk/monitor/".$calls_row['recording'].".wav")){
    $wavefile="
          <form>
              <input type=\"button\" value=\"Play\" onClick=\"PlaySound('clip-".$stat_id."')\" />
          </form>
          <embed src=\"pass-thru.php?f=".$calls_row['recording'].".wav\" autostart=\"false\" width=\"0\" height=\"0\" id=\"clip-".$stat_id."\" enablejavascript=\"true\" />
        ";
}

声音剪辑存储在网络根目录之外的一个目录中,我被告知我们无法更改。由于无法在本地获取文件,我编写了一个小的 php 传递脚本,将文件传递回我的网页。

声音片段在播放之前使用一小段 JavaScript 来获取指定文件的链接 ID;

function PlaySound(soundobj) {
  var thissound=document.getElementById(soundobj);
  thissound.Play();
}

这些文件在 webroot 外部访问并通过以下 php 代码传递到 html;

$basedir = '/var/spool/asterisk/monitor';
$clip = $_GET['f'];
$file = $basedir.'/'.$clip;
$ext = array_pop(explode ('.', $file));
if(file_exists($file) && $ext != '' && isset($allowed[strToLower($ext)])) {
    $type = $allowed[strToLower($ext)];
}
header("Content-type: audio/x-wav");
header("Content-Disposition: filename='{$file}'");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file));
readfile($file);
exit();

一切都正常工作,剪辑被传递,声音剪辑在浏览器中播放。

问题是,当页面加载时,所有声音剪辑都会加载到缓存中,这显然会减慢速度。我对结果进行了分页,因此一次只能下载 25 个,但我真的很希望只提供可用声音剪辑的链接,并且仅在选择播放时才下载它们。

任何想法将不胜感激。

I have been scratching my head with this one for a few days,

I am building a web app for a client associated with call tracking. One aspect of the system is to allow for short recorded voicemail messages to be played directly from a "play" button beside the message details.

I am accessing the sound files as such;

$wavefile=$calls_row['recording'];
if (file_exists("/var/spool/asterisk/monitor/".$calls_row['recording'].".wav")){
    $wavefile="
          <form>
              <input type=\"button\" value=\"Play\" onClick=\"PlaySound('clip-".$stat_id."')\" />
          </form>
          <embed src=\"pass-thru.php?f=".$calls_row['recording'].".wav\" autostart=\"false\" width=\"0\" height=\"0\" id=\"clip-".$stat_id."\" enablejavascript=\"true\" />
        ";
}

The sound clips are stored outside the web root in a directory I have been told we cannot change. Being unable to get at the files locally I put together a small php pass-through script to pass me the file back to my web page.

The sound clip is using a small piece of javascript to get the ID of the link for the specified file before playing it;

function PlaySound(soundobj) {
  var thissound=document.getElementById(soundobj);
  thissound.Play();
}

The files are accessed outside the webroot and passed to the html by the following php code;

$basedir = '/var/spool/asterisk/monitor';
$clip = $_GET['f'];
$file = $basedir.'/'.$clip;
$ext = array_pop(explode ('.', $file));
if(file_exists($file) && $ext != '' && isset($allowed[strToLower($ext)])) {
    $type = $allowed[strToLower($ext)];
}
header("Content-type: audio/x-wav");
header("Content-Disposition: filename='{$file}'");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file));
readfile($file);
exit();

Everything works fine as it should, the clips are passed through and the sound clips play in the browser.

The problem is that all of the soundclips are loaded into the cache when the page loads, obviously slowing things down a bit. I have the results paginated so its only downloading 25 at a time but I would really love to just have the links to the soundclips available and that they are only downloaded when selected to be played.

Any ideas would be appreciated.

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

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

发布评论

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

评论(1

囚我心虐我身 2024-11-25 02:41:58

我能想到的最简单的选择是不在 HTML 中嵌入声音文件。据我所知,老式的 embed 元素没有关闭声音文件预加载的选项,对于此功能,您应该查看 HTML5 的 audio (这不是当然是在旧的 IE 中工作)。

因此,基本上,您只需不将 embed 元素放入 HTML 代码中,当单击“播放”按钮时,就会从 Javascript 插入 embed 元素。创建 embed 元素所需的数据(例如 URL)可以存储在 data- 属性中,或者在您的情况下,只需写入内联事件处理程序的参数列表。

如果您需要关心关闭了 Javascript 的用户,您仍然可以在 noscript 标记中包含 embed 元素(希望它不会开始预加载)。

对于此方法,您只需要一些基本功能,例如 document.createElement()insertBefore()

The easiest option I can think of is not embedding the sound file in the HTML. As far as I know, the old-school embed element has no options to turn off preloading of sound files, for this feature you should look into HTML5's audio (which is not working in old IEs of course).

So basically, you just don't put an embed element into your HTML code, and when the Play button is clicked, you insert the embed element from Javascript. The data you need for the embed element to be created (URL for example) can be stored in a data- attribute, or in your case, simply written to the inline event handler's parameter list.

If you need to care about users with Javascript turned off, you can still include the embed element in a noscript tag (hopefully it won't start preloading).

For this method you only need some basic functions like document.createElement() and insertBefore().

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