通过php的mp4文件不能作为html5视频播放 [英] mp4 file through php not playing as html5 video
问题描述
我正在尝试通过PHP输出mp4视频文件. 当通过Flash播放器(例如flowplayer)使用它时,效果很好. 但是,当我尝试将其用作html5视频标签上的源代码或直接调用php文件时,它将无法正常工作.
I am trying to output an mp4 video file through PHP. When it is used through a flash player (eg. flowplayer) it is working great. But when I'm trying to use it as a source on an html5 video tag or to call directly the php file, it doesn't work.
我使用的代码如下:
$filesize = filesize($file);
header("Content-Type: video/mp4");
if ( empty($_SERVER['HTTP_RANGE']) )
{
header("Content-Length: $filesize");
readfile($file);
}
else //violes rfc2616, which requires ignoring the header if it's invalid
{
rangeDownload($file);
}
和rangeDownload
功能来自 http://mobiforge.com/developing /story/content-delivery-mobile-devices 附录A.
即使我使用Content-Range
标头(Content-Range:bytes 0-31596111/31596112
),它也停留在下载30.13 MB的视频上.
Even when I use a Content-Range
header (Content-Range:bytes 0-31596111/31596112
), it stucks on downloading 30.13 MB of the video.
推荐答案
最后,我找到了一种使之工作的方法
Finally I've found a way to make it work
header("Content-Type: $mediatype");
if ( empty($_SERVER['HTTP_RANGE']) )
{
header("Content-Length: $filesize");
$fh = fopen($file, "rb") or die("Could not open file: " .$file);
# output file
while(!feof($fh))
{
# output file without bandwidth limiting
echo fread($fh, $filesize);
}
fclose($fh);
}
else //violes rfc2616, which requires ignoring the header if it's invalid
{
rangeDownload($file);
}
它在php文件的直接链接和html5视频标记内起作用.
但是,为了在Flowplayer(和其他Flash/html5播放器)中工作,您需要添加mp4扩展名(例如view.php?id = XXX& file = type.mp4)
It is working in direct link of the php file and inside html5 video tag.
But in order to work in Flowplayer (and maybe in other flash/html5 players) you need to add a mp4 extension (eg. view.php?id=XXX&file=type.mp4)
这篇关于通过php的mp4文件不能作为html5视频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!