Welcome Guest, Not a member yet? Register   Sign In
Show MP3 file inline - no download dialog!
#1

[eluser]Matt Larsen[/eluser]
Hey,

I'm trying to create an "embed file" script. For example:

Code:
<embed src="http://mysite.com/embed/1"></embed>

The script works perfectly for images. One of the files i'm testing is an MP3. This file type doesn't seem to work. I've executed the URL, and I get a download dialog. I've assumed this to be the problem. When I execute the URL with the image file type, I simply get the image (no download dialog).

Here's my code:

Code:
function embed($file_id = 0)
{
  $file = $this->files->get($file_id);
  
  if($file)
  {
     $mime = $this->mimes->get($file['ext']); //mp3's are: audio/mpeg3. I've double checked that this mime actually gets used as the header.
     if(!$mime) {
        $mime['mime'] = 'application/octet-stream';
     }

     header('Content-type: ' . $mime['mime']);
     header('Content-disposition: inline; filename="' . $file['name'] . '.' . $file['ext'] . '"');
                
     header('Accept-Ranges: bytes');
     header('Content-Length: ' . filesize('./files/' . $file['filename']));
     header('Keep-Alive: timeout=5, max=100');
     header('Connection: Keep-Alive, Keep-Alive');
                
     readfile(base_url() . 'files/' . $file['filename']);
  }
}

Are there any other headers I should include? Or is there a header I need to change? Thanks very much for your time,
Matt
#2

[eluser]AlexJ[/eluser]
Only image and plaintext files and files the browser know, will be opened in a browser window (I think IE is the only exception to this, it also downloades plaintext).

So if the mimetype is not in a certain browserlist (like PDF, plaintext, images) etc, it wont work

This is more a HTTP problem than a CI problem
#3

[eluser]ranjudsokomora[/eluser]
Matt Larsen,
I wouldn't suggest streaming an MP3 via Mime type to the browser. You might want to try to use an external player such as JWFlash. You could then build a very simple view that would play the file, of course you will have to sent it a location.

Just a thought.
#4

[eluser]Matt Larsen[/eluser]
Thanks guys. I'll look into JWFlash. By the way, I figured it out! "audio/mpeg3" isn't completely correct. Apparently, it's supposed to be "audio/mpeg" (no 3 on the end).

Cheers,
Matt




Theme © iAndrew 2016 - Forum software by © MyBB