简体   繁体   中英

embed mp4 video in browser using .net ashx and querystring

I am tasked with using .net ashx to be able to serve videos to users via web browser.
The videos are stored separately in another drive on the same server. F:\\Videos. I have been successful at getting wmv video to work, but cannot seem to get mp4 format to play. I have tried multiple context types - video/mpeg, video/x-fla, video/mp4. When I use video/x-ms-wmv to play wmv files, Windows media player pops up and plays the video just fine. However, I was told that I can't use wmv because not all of our user base may be able to use Windows Media Player.

Can someone tell me what is missing / wrong about the code so that I can play mp4 videos?

In my web.config - I have the following:

<urlMappings enabled="true">
  <add url="~/Video.aspx" mappedUrl="~/VideoHandler.ashx"/>
</urlMappings>

I have an .aspx file with a hyperlink to direct the user to the video:

<asp:HyperLink ID="Hyperlink2" runat="server" NavigateUrl="~/Video.aspx?File=WRF" Target="_parent">WRF<asp:HyperLink>

My .ashx file has the following:

public void ProcessRequest(HttpContext context)
{
    string file = context.Request.QueryString["file"];
    if (file != String.Empty)
    {
        context.Response.ContentType = "video/mp4";
        switch (file)
        {
            case "WRF":
                context.Response.WriteFile("f:\\Videos\\WRF.mp4");                       
                break;
            case "INTRO":
                context.Response.WriteFile("f:\\Videos\\Intro.mp4");
                break;
        }
    }
    else
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("No file requested");
    }
}

Wow, where to beging...

You need to think of yourself and your code merely as a plate on which you are serving a video file to be played in a player.

The most important question here is: What really is the format of your MP4 video? The ContentType or the file extension do not tell us anything about the encoding.

It could very well be that your user base may not be able to play your particular video clips.

So... what is happening? What is popping up, is it throwing errors?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM