最近做了一个小项目,其中有一个功能是播放Flv文件,Flv文件存放在服务器的非网站目录下,不能使用虚拟路径进行访问,同时由于视频文件超过1OOM,因此播放的时候同时需要考虑到内存的占用,最终采用后台获取文件输出到Response,使用 Vcastr22.swf 播放器进行播放.具体代码如下:
protected void Page_Load(object sender, EventArgs e) { int SubjectID = ConvertHelper.ObjectToInt(RequestHelper.GetQueryString("ID"), 0); var LoginUser = CustomBase.GetLoginUser(); if (LoginUser == null) { Response.Write("用户未登陆!"); Response.End(); return; } var Subject = BusinessLogic.SubjectService.GetSubject(SubjectID); if (Subject == null) { Response.Write("找不到该课程!"); Response.End(); return; }; var userList = BusinessLogic.UserService.GetUserCategory(LoginUser.ID); if (userList.Count(n => n.ID.Equals(Subject.CategoryID)) == 0) { Response.Write("用户未开通该课程!"); Response.End(); return; } if (DirFileHelper.IsExistFile(Subject.Vedio)) { //输出到浏览器 try { const long ChunkSize = 102400; byte[] buffer = new byte[ChunkSize]; Response.Clear(); System.IO.FileStream iStream = System.IO.File.OpenRead(Subject.Vedio); long dataLengthToRead = iStream.Length; var ext = DirFileHelper.GetExtension(Subject.Vedio); if (ext.ToLower().Equals(".swf")) { Response.ContentType = "application/x-shockwave-flash"; } else if (ext.ToLower().Equals(".flv")) { Response.ContentType = "vedio/x-flv"; } while (dataLengthToRead > 0 && Response.IsClientConnected) { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize)); Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; } Response.Close(); } catch (Exception ex) { Response.Write(ex.Message); } } }