使用 ASP.NET 流式传输视频

发布于 2024-09-17 12:42:33 字数 1439 浏览 4 评论 0原文

有没有一种好的方法可以通过 ASP.NET 将视频流式传输到普通网页和移动设备?我已尝试以下方法,但在我的索尼爱立信 K810i 上不起作用。当我在浏览器中尝试时,我可以看到该剪辑(但不知道它是否正在流式传输)。

html:

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

FileHandler.ashx (在 ASP.NET 中流式传输文件的最佳方式< /a>):

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();

}

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();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-09-24 12:42:33

你所拥有的并不是“技术上”的流媒体。这是一个文件下载。您的客户端(浏览器/手机)发送了一个 HTTP 请求,您的 FileHandler.ashx 打开该文件并将字节写入响应流。这与网页请求的交互完全相同,只不过数据是 html 文本而不是表示视频的二进制数据。

如果手机不支持视频,则可能是编码不兼容。如果您确定手机可以播放视频,请查看手机是否需要渐进式下载支持(例如 iPhone / iPad / iPod Touch 需要媒体播放器“流式传输”视频。)如果是这样,您将需要查看可用于处理字节范围数据请求并使用指定范围内的文件中的字节响应请求的许多解决方案中的任何一个。

我编写了一个 ASP.NET MVC 库来处理这个问题,我的工作主要是基于这个指南和源代码

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文