Javascript PHP视频流Seekbar在Chrome中不可用
这与我的另一篇PHP视频流帖子有些关联,但这次的问题是视频的seekbar在Chrome中不起作用 我在Stack Overflow上找到了几篇关于它的帖子,但是没有一篇能够解决这个问题。我会把它们都链接起来,但我似乎找不到和昨天一样的帖子 我将列出PHP代码的两个版本。我还应该指出在PHP加载视频数据之前我到底在做什么。在HTML页面上,我有一个Javascript PHP视频流Seekbar在Chrome中不可用,javascript,php,google-chrome,video,seekbar,Javascript,Php,Google Chrome,Video,Seekbar,这与我的另一篇PHP视频流帖子有些关联,但这次的问题是视频的seekbar在Chrome中不起作用 我在Stack Overflow上找到了几篇关于它的帖子,但是没有一篇能够解决这个问题。我会把它们都链接起来,但我似乎找不到和昨天一样的帖子 我将列出PHP代码的两个版本。我还应该指出在PHP加载视频数据之前我到底在做什么。在HTML页面上,我有一个标记,没有标记。我使用Javascript对包含源标记的PHP文件进行AJAX调用。源标记本身不包含指向视频源文件的直接链接。相反,它们引用另一个加载
标记,没有
标记。我使用Javascript对包含源标记的PHP文件进行AJAX调用。源标记本身不包含指向视频源文件的直接链接。相反,它们引用另一个加载数据的PHP文件
视频的顶级HTML。超级简单
<video id="showvideo" height="540" width="864" controls></video>
页面加载时,Javascript函数将加载
以下是firstphpfile.php的内容
<?php
echo "
<source src=\"http://example.com/video1.php?type=stuff.mp4\" type=\"video/mp4\">
<source src=\"http://example.com/video2.php?type=stuff.ogv\" type=\"video/ogg\">
";
?>
再说一次,没什么大不了的。现在,我将发布两个不同版本的video1.php文件,它实际上占用了文件资源
第1版:
<?php
$file = video.mp4;
$filesize = filesize($file);
$offset = 0;
$length = $filesize;
if ( isset($_SERVER['HTTP_RANGE']) ) {
// if the HTTP_RANGE header is set we're dealing with partial content
$partialContent = true;
// find the requested range
// this might be too simplistic, apparently the client can request
// multiple ranges, which can become pretty complex, so ignore it for now
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = intval($matches[2]) - $offset;
} else {
$partialContent = false;
}
$file = fopen($file, 'r');
// seek to the requested offset, this is 0 if it's not a partial conten request
fseek($file, $offset);
$data = fread($file, $length);
fclose($file);
if ( $partialContent ) {
// output the right headers for partial content
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
}
// output the regular HTTP headers
header("Content-Type:video/mp4");
header('Content-Length: $filesize');
header('Accept-Ranges: bytes');
// don't forget to send the data too
print($data);
?>
因此,虽然两者都能顺利播放视频,但只有Firefox版本允许我进行任何形式的搜索。第二个版本让你只能向后搜索,我更喜欢这样
我尝试了另一个版本,但在写这篇文章之前我已经删除了代码,没有再找到它
我不确定我做错了什么,也没有找到解决方案来解决允许Chrome版视频搜索的问题。好的,所以我终于让它工作了。我决定不使用javascript加载php文件
另外,我去掉了mime类型变量,只是正确地设置了头。我发现为mime类型使用变量会导致浏览器为内容类型头加载错误的mime类型,从而导致视频资源失败。好的,所以我终于让它工作了。我决定不使用javascript加载php文件 另外,我去掉了mime类型变量,只是正确地设置了头。我发现对mime类型使用变量会导致浏览器为内容类型头加载错误的mime类型,从而导致视频资源失败
<?php
$file = video.mp4;
$filesize = filesize($file);
$offset = 0;
$length = $filesize;
if ( isset($_SERVER['HTTP_RANGE']) ) {
// if the HTTP_RANGE header is set we're dealing with partial content
$partialContent = true;
// find the requested range
// this might be too simplistic, apparently the client can request
// multiple ranges, which can become pretty complex, so ignore it for now
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = intval($matches[2]) - $offset;
} else {
$partialContent = false;
}
$file = fopen($file, 'r');
// seek to the requested offset, this is 0 if it's not a partial conten request
fseek($file, $offset);
$data = fread($file, $length);
fclose($file);
if ( $partialContent ) {
// output the right headers for partial content
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
}
// output the regular HTTP headers
header("Content-Type:video/mp4");
header('Content-Length: $filesize');
header('Accept-Ranges: bytes');
// don't forget to send the data too
print($data);
?>
<?php
$file = video.mp4;
$mime = "video/mp4"; // The MIME type of the file, this should be replaced with your own.
$size = filesize($file); // The size of the file
// Send the content type header
header('Content-type: ' . $mime);
// Check if it's a HTTP range request
if(isset($_SERVER['HTTP_RANGE'])){
// Parse the range header to get the byte offset
$ranges = array_map(
'intval', // Parse the parts into integer
explode(
'-', // The range separator
substr($_SERVER['HTTP_RANGE'], 6) // Skip the `bytes=` part of the header
)
);
// If the last range param is empty, it means the EOF (End of File)
if(!$ranges[1]){
$ranges[1] = $size - 1;
}
// Send the appropriate headers
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Length: ' . ($ranges[1] - $ranges[0])); // The size of the range
// Send the ranges we offered
header(
sprintf(
'Content-Range: bytes %d-%d/%d', // The header format
$ranges[0], // The start range
$ranges[1], // The end range
$size // Total size of the file
)
);
// It's time to output the file
$f = fopen($file, 'rb'); // Open the file in binary mode
$chunkSize = 8192; // The size of each chunk to output
// Seek to the requested start range
fseek($f, $ranges[0]);
// Start outputting the data
while(true){
// Check if we have outputted all the data requested
if(ftell($f) >= $ranges[1]){
break;
}
// Output the data
echo fread($f, $chunkSize);
// Flush the buffer immediately
@ob_flush();
flush();
}
}
else {
// It's not a range request, output the file anyway
header('Content-Length: ' . $size);
// Read the file
@readfile($file);
// and flush the buffer
@ob_flush();
flush();
}
?>