开发者

Stream Video with ASP.NET

Is there a good way to stream video through asp.net to a normal webpage and mobile? I've tried the following but it doesn't work in my Sony Ericsson K810i. When I try it in my browser, I can see开发者_高级运维 the clip (don't know if it's streaming though).

html:

<object type="video/3gpp" 
        data="handlers/FileHandler.ashx" 
        id="player" 
        width="176" 
        height="148" 
        autoplay="true"></object>

FileHandler.ashx (Best way to stream files in ASP.NET):

public void ProcessRequest (HttpContext context) {

    string path = "~/files/do.3gp";

    string localPath = context.Server.MapPath(path);

    if (!File.Exists(localPath))
    {
        return;
    }

    // get info about contenttype etc 
    FileInfo fileInfo = new FileInfo(localPath);
    int len = (int)fileInfo.Length;
    context.Response.AppendHeader("content-length", len.ToString());
    context.Response.ContentType = FileHelper.GetMimeType(fileInfo.Name); // returns video/3gpp

    // stream file
    byte[] buffer = new byte[1 << 16]; // 64kb
    int bytesRead = 0;
    using(var file = File.Open(localPath, FileMode.Open))
    {
       while((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0)
       {
            context.Response.OutputStream.Write(buffer, 0, bytesRead);
       }
    }

    // finish
    context.Response.Flush();
    context.Response.Close();
    context.Response.End();

}


What you've got isn't "technically" streaming. It's a file download. Your client (browser/phone) sent an HTTP request and your FileHandler.ashx opened the file and wrote the bytes into the response stream. This is exactly the same interaction for a web page request except the data is html text rather than binary data representing a video.

If the phone isn't supporting the video it might be incompatible encoding. If you're sure the video is playable by the phone, see if the phone wants progressive download support (like the iPhone / iPad / iPod Touch require for the media player to "stream" videos.) If this is so, you'll need to look at any of a number of solutions that are available for handling requests for byte-range data and responding to the request with the bytes from the file in the range specified.

I wrote a library for ASP.NET MVC to handle this and my work was mostly done based on this guidance and source code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜