2

What im trying to do is play a .mp4 file in JW Player that is given by a database in bytes.

So here is my Script for JW Player:

<script type="text/javascript"> 
    jwplayer("contvideo").setup({ 
    flashplayer:"jwplayer/player.swf", 
    file: "downloadvideo.aspx", 
    provider: "video", 
    height: 140, 
    width: 188, 
    skin: "jwplayer/slim.zip", 
    modes: [{type:'flash',src:'jwplayer/player.swf'},{type:'html5'},{type:'download'}] }); 
</script>

The script recives its movie file from the web page service downloadvideo.aspx.

The code for downloadvideo.aspx is:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim bytes() As Byte = System.IO.File.ReadAllBytes("C:\websites\videospecialty\wdo.mp4")

    Response.Clear()
    Response.AddHeader("Content-Disposition", "attachment;filename=wdo.mp4")
    Response.ContentType = "video/mp4"
    Response.Flush()

    Response.OutputStream.Write(bytes, 0, bytes.Length)
    Response.OutputStream.Flush()

    Response.End()

End Sub

But when i try this JW Player errors and says that they video was not found.

Any help with this would be greatly appriciated.

Necro
  • 333
  • 5
  • 18

1 Answers1

5

It seems that JW Player figures out the kind of file to play based on the file extension in the file option. Because you are streaming these from disk, via an ASPX page, you don't have one.

The following is how I got this working with JW Player 6 (using mp3s - I didn't have any mp4s handy):

jwplayer("player1").setup({
    flashplayer: "/jwplayer/jwplayer.flash.swf",
    file: '/Streamer.ashx',
    height: 340,
    width: 388,
    type: 'mp3'
});

The key was the use of type to indicate what kind of file was on the other end. The only reference I can offer for this is here: it dates back to 2007, but I couldn't seem to find mentions of type in the documention.

Inspired by: How do I stream .flv files from SQL database, I implemeted an HTTP handler to do the streaming:

public class Streamer : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string filePath = "my file path";
        byte[] buffer = File.ReadAllBytes(filePath);

        context.Response.Clear();
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Cache.SetLastModified(DateTime.Now);
        context.Response.AppendHeader("Content-Type", "audio/mpeg3");
        context.Response.AddHeader("Content-Disposition", "attachment;filename=file.mp3");
        context.Response.AppendHeader("Content-Length", buffer.Length.ToString());

        context.Response.BinaryWrite(buffer);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Note that an ASPX page would probably work fine as well, though handlers are more appropriate for this kind of task (and should be more efficient as they don't have a page cycle).

Reading the whole file into memory isn't ideal, but it sufficed for proving the concept.

Community
  • 1
  • 1
nick_w
  • 14,758
  • 3
  • 51
  • 71