开发者

How do I add sequential access to my video stream?

开发者 https://www.devze.com 2023-02-16 00:57 出处:网络
I\'m streaming .flv videos from my SQL Server with the code below. But from what I understand the whole video will be loaded into memory before being played. What I would like to do is add CommandBeha

I'm streaming .flv videos from my SQL Server with the code below. But from what I understand the whole video will be loaded into memory before being played. What I would like to do is add CommandBehavior.SequentialAccess to the SQLDataReader... but I can't get it to work.

Please help me here. If you could provide a working example that is applicable to mine, I'd be happy. Psuedo code would be the second best alternative. Any pointers are appreciated though.

Here's my httpHandler

public class ViewFilm : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            // Check if video id was given
            if (context.Request.QueryString["id"] != null)
            {
                string video_ID = context.Request.QueryString["id"];

                // Connect to DB and get the video
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
                using (SqlCommand cmd = new SqlCommand("GetVideo", con))开发者_如何学Go
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter sqlParam = cmd.Parameters.Add("@videoId", SqlDbType.Int);
                    sqlParam.Value = video_ID;

                    con.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr.HasRows)
                        {
                            dr.Read();
                            // Add HTTP header: cache, content type and length
                            context.Response.Cache.SetCacheability(HttpCacheability.Public);
                            context.Response.Cache.SetLastModified(DateTime.Now);
                            context.Response.AppendHeader("Content-Type", "video/x-flv");
                            context.Response.AppendHeader("Content-Length", ((byte[])dr["data"]).Length.ToString());
                            context.Response.BinaryWrite((byte[])dr["data"]);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
    }

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


First: don't stream from the database.

Second: if you really WANT to stream from the database, here is the example how you might do it. But don't. Really. Store pointers (file paths) in the database and leave streaming handling to ISS or whatever server you use.

0

精彩评论

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

关注公众号