PHP服务MP4 – Chrome“临时标题显示/请求尚未完成”错误
作者:互联网
我想在允许用户看到视频之前检查用户的订阅,因此我使用PHP与Stripe进行交互以检查用户的订阅,并使用PHP script将MP4提供给浏览器
首次在Google Chrome中播放视频时使用效果很好(使用HTML5播放器)…
但是,当我关闭视频并再次播放时,视频不再播放…我也无法重新加载当前页面.就像服务器停止工作一样.
当我检查第一个视频请求(播放的那个)时,在Timing选项卡中我看到:“注意:请求尚未完成!” (截图如下)
当我检查第二个视频请求(没有播放的那个)时,在Headers选项卡中显示“[警告标志]临时标题显示”(截图如下)
一切都在Safari或Firefox中按预期工作
任何人都知道发生了什么事吗?视频再次播放的唯一方法是关闭当前标签,再次进入该网站.重新加载不起作用!
解决方法:
我建议你使用以下函数而不是当前的’流脚本’.如果你传递$filename_output,它将作为下载服务该文件,否则它将流传输!
它应该适用于每个浏览器.
serveFile( ‘/哪里/我/ vid.mp4’);
public function serveFile($filename, $filename_output = false, $mime = 'application/octet-stream')
{
$buffer_size = 8192;
$expiry = 90; //days
if(!file_exists($filename))
{
throw new Exception('File not found: ' . $filename);
}
if(!is_readable($filename))
{
throw new Exception('File not readable: ' . $filename);
}
header_remove('Cache-Control');
header_remove('Pragma');
$byte_offset = 0;
$filesize_bytes = $filesize_original = filesize($filename);
header('Accept-Ranges: bytes', true);
header('Content-Type: ' . $mime, true);
if($filename_output)
{
header('Content-Disposition: attachment; filename="' . $filename_output . '"');
}
// Content-Range header for byte offsets
if (isset($_SERVER['HTTP_RANGE']) && preg_match('%bytes=(\d+)-(\d+)?%i', $_SERVER['HTTP_RANGE'], $match))
{
$byte_offset = (int) $match[1];//Offset signifies where we should begin to read the file
if (isset($match[2]))//Length is for how long we should read the file according to the browser, and can never go beyond the file size
{
$filesize_bytes = min((int) $match[2], $filesize_bytes - $byte_offset);
}
header("HTTP/1.1 206 Partial content");
header(sprintf('Content-Range: bytes %d-%d/%d', $byte_offset, $filesize_bytes - 1, $filesize_original)); ### Decrease by 1 on byte-length since this definition is zero-based index of bytes being sent
}
$byte_range = $filesize_bytes - $byte_offset;
header('Content-Length: ' . $byte_range);
header('Expires: ' . date('D, d M Y H:i:s', time() + 60 * 60 * 24 * $expiry) . ' GMT');
$buffer = '';
$bytes_remaining = $byte_range;
$handle = fopen($filename, 'r');
if(!$handle)
{
throw new Exception("Could not get handle for file: " . $filename);
}
if (fseek($handle, $byte_offset, SEEK_SET) == -1)
{
throw new Exception("Could not seek to byte offset %d", $byte_offset);
}
while ($bytes_remaining > 0)
{
$chunksize_requested = min($buffer_size, $bytes_remaining);
$buffer = fread($handle, $chunksize_requested);
$chunksize_real = strlen($buffer);
if ($chunksize_real == 0)
{
break;
}
$bytes_remaining -= $chunksize_real;
echo $buffer;
flush();
}
}
标签:php,stripe-payments,google-chrome,video-streaming,http-headers 来源: https://codeday.me/bug/20190929/1831256.html