How to stream video content in asp.net?

后端 未结 3 1370
刺人心
刺人心 2020-12-24 08:28

I have the following code which downloads video content:

WebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse wresp = (HttpWebRes         


        
相关标签:
3条回答
  • 2020-12-24 08:56

    This is more efficient for you especially if you need to stream a video from a file on your server or even this file is hosted at another server

    File On your server:

    context.Response.BinaryWrite(File.ReadAllBytes(HTTPContext.Current.Server.MapPath(_video.Location)));
    

    File on external server:

    var wc = new WebClient();
        context.Response.BinaryWrite(wc.DownloadData(new Uri("http://mysite/video.mp4")));
    
    0 讨论(0)
  • 2020-12-24 09:05

    You're reading the entire file into a single buffer, then sending the entire byte array at once.

    You should read into a smaller buffer in a while loop.

    For example:

    byte[] buffer = new byte[4096];
    
    while(true) {
        int bytesRead = myStream.Read(buffer, 0, buffer.Length);
        if (bytesRead == 0) break;
        Response.OutputStream.Write(buffer, 0, bytesRead);
    }
    
    0 讨论(0)
  • 2020-12-24 09:12

    Have you looked at Smooth Streaming?

    Look at sample code here

    0 讨论(0)
提交回复
热议问题