Laravel 4.2 播放 mp3 - 不可缠绕

Laravel 4.2 serving mp3 - not windable

我在使用 Laravel 4.2 播放 mp3 时遇到一些问题 我有一些文件需要用flashplayer播放。

    public function get($filename)
    {
        $file = new Symfony\Component\HttpFoundation\File\File(storage_path().DbConfig::get('system.upload_dir').'/'.DbConfig::get('system.upload_music').'/'.$filename);

        $response =  Response::make(file_get_contents(storage_path().DbConfig::get('system.upload_dir').'/'.DbConfig::get('system.upload_music').'/'.$filename));
        $response->header('Content-Type', $file->getMimeType());
        $response->header('Content-Length', $file->getSize());
        $response->header('Content-Transfer-Encoding', '');
        $response->header('Accept-Range', 'bytes');
        $response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
        $response->header('Connection', 'Keep-Alive');
        return $response;
    }

这提供文件 - 如果在 chrome 中打开它会启动 chrome 的默认播放器并播放音乐,当我用 flashplayer 播放它时也是如此。

但是我没法给唱片上弦。 如果我使用 apache(而不是 Laravel 控制器)提供文件,它工作正常。

如果有人能帮助我解决这个问题,我将不胜感激。

更新

Headers 当通过 Laravel 服务时:

HTTP/1.1 200 OK
Date: Thu, 01 Oct 2015 18:43:59 GMT
Server: Apache/2
Cache-Control: must-revalidate, post-check=0, pre-check=0, private
Content-Transfer-Encoding: 
Accept-Range: bytes
Connection: Keep-Alive, Keep-Alive
Set-Cookie: laravel_session=eyJ[...]D; expires=Thu, 01-Oct-2015 20:44:00 GMT; Max-Age=7200; path=/; httponly
Vary: Accept-Encoding,User-Agent
Keep-Alive: timeout=2, max=100
Transfer-Encoding: chunked
Content-Type: audio/mpeg

Headers 在没有 Laravel 的情况下提供:

HTTP/1.1 200 OK
Date: Thu, 01 Oct 2015 18:51:16 GMT
Server: Apache/2
Last-Modified: Fri, 13 Mar 2015 04:03:23 GMT
ETag: "ead61-5112394e338c0"
Accept-Ranges: bytes
Content-Length: 961889
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: audio/mpeg

对我来说,header 的主要区别是在使用 Laravel 时没有 Content-Length。我不知道 Flash 播放器是如何工作的,但我敢说它需要知道文件的长度才能搜索(即风)到任何位置。

但是,您发布的代码明确设置了 header。搜索了一下,我发现了这个 Laravel 问题:2079。他们建议使用未记录的 Response::stream 发送文件内容,如下所示:

Response::stream(function() use($fileContent) { echo $fileContent; }, 200, $headers);

这是 Symfony doc for StreamedResponse

响应类型更新

您提供的是静态文件,因此要使用的适当响应类型是 BinaryFileResponse through Laravel's Response::download($pathToFile, $name, $headers)

(顺便说一下,您可以使用第三个参数来设置 headers)

更新于 Content-Length

Symfony's Response class 的来源掌握着 Content-Length 被删除的关键:

/**
 * Prepares the Response before it is sent to the client.
 *
 * This method tweaks the Response to ensure that it is
 * compliant with RFC 2616. Most of the changes are based on
 * the Request that is "associated" with this Response.
 *
 * @param Request $request A Request instance
 *
 * @return Response The current response.
 */
public function prepare(Request $request)
{
    $headers = $this->headers;
    if ($this->isInformational() || $this->isEmpty()) {
        // [snip]
    } else {
        // [snip]
        // Fix Content-Length
        if ($headers->has('Transfer-Encoding')) {
            $headers->remove('Content-Length');
        }
        // [snip]
    }
    // [snip]
}

RFC 2616 不允许同时使用 Transfer-EncodingContent-Length header,Symfony 强制执行此操作。引用:

4.4 Message Length

[...]

3.If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored.

此外,根据此 SO questionContent-Transfer-Encoding 仅用于电子邮件。否则使用 Transfer-Encoding

另一件事:您在 Laravel 中使用了 Accept-Range 而不是 Apache 中的 Accept-Ranges

最后,尝试不设置任何 header 的简单 download 响应,看看您得到了什么。然后根据需要添加更多 header。