Serve mp3 file from laravel for iOS AvPlayer

2019-02-16 00:51发布

问题:

I am able to serve my mp3 file from a controller, but the file served from my controller will not get played in ios AvPlayer (using url). I'm sure that my client code is fine, since it can play songs from other servers. also you may check my server from the link that I wrote at the end and see that my controller is serving the file.

should I add any specific header to the response for AvPlayer to support my url ?

my laravel controller code now:

    $response = new BinaryFileResponse($file);
    BinaryFileResponse::trustXSendfileTypeHeader();

    return $response;

here is a sample link that works how i want http://s3.amazonaws.com/kargopolov/BlueCafe.mp3

and here is mine that not works http://projectm.e-rundev.ir/api/song/download/11.mp3

any directions would be appreciated in advance

回答1:

here is the code that worked

    $file = storage_path() . '/upload/song/' . $filename;
    $mime_type = "audio/mpeg";
    $headers = array(
        'Accept-Ranges: 0-' . (filesize($file) -1) ,

        'Content-Length:'.filesize($file),
        'Content-Type:' . $mime_type,
        'Content-Disposition: inline; filename="'.$filename.'"'

    );
    $fileContents = File::get($file);
    return Response::make($fileContents, 200, $headers);