opening mp4 via php results in full download before playback

心已入冬 提交于 2019-12-11 11:23:52

问题


I'm trying to feed an mp4 file to flash player via php and the video is downloaded completely before starting playback.

$src = '/var/www/user/data/www/domain.com/video.mp4';
if(file_exists($src) and is_readable($src)) {
    header('Content-Type: video/mp4');
    header('Content-Length: '.filesize($src));
    readfile($src);
} else die('error');

I've tried curl with similar results. Any ideas what's causing this delay?


回答1:


Most likely your Flash player is hoping you'll handle HTTP Range requests so it can get started faster on the playback.

The HTML5/Flash audio player jPlayer has a section in their developer guide about this. Scroll to the part about Byte-Range Requests:

Your server must enable Range requests. This is easy to check for by seeing if your server's response includes the Accept-Ranges in its header.

Also note that they offer a PHP solution for handling Range requests if you have to use PHP instead of a direct download.

smartReadFile.php
https://groups.google.com/forum/#!msg/jplayer/nSM2UmnSKKA/bC-l3k0pCPMJ




回答2:


Another option would be to just have apache send the file it self as opposed to reading it in php and dumping it to the output using X-Sendfile.

First make sure apache is compiled with sendfile support then alter your output code to be:

header ('X-Sendfile: ' . $src);
header ('Content-Type:  video/mp4');
header ('Content-Disposition: attachment; filename="' . $filename . '"');
exit;

This is normally faster than doing it via PHP.



来源:https://stackoverflow.com/questions/12695586/opening-mp4-via-php-results-in-full-download-before-playback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!