windows下IIS让flv视频网站进度条随意拖放[C#]

在做一个视频网站项目时,要求视频能够任意拖动播放,即使是没有缓冲完也需要

网上查了下资料,主要需要3点

  1. 播放器要支持
  2. flv视频要有关键帧和meta信息
  3. 服务器端要支持

首先,播放器的问题。
我用的播放器是flowplayer 3.2.7 下载地址:http://releases.flowplayer.org/flowplayer/flowplayer-3.2.7.zip 
这个播放器默认是不支持拖放的,还需要插件,下载地址是:http://releases.flowplayer.org/flowplayer.pseudostreaming/flowplayer.pseudostreaming-3.2.7.zip
解压之后,将插件文件夹里的两个flash文件拷到播放器文件夹下。
然后将example文件夹下的javascript脚本改为如下情形

   <script>
      flowplayer("player", "../flowplayer-3.2.7.swf", {
          // configure clip to use "lighthttpd" plugin for providing video data
          clip: {
           url: 'http://pseudo01.hddn.com/vod/demo.flowplayervod/Extremists.flv',
        autoPlay:false , url: 't4.flv',
              provider: 'lighttpd'
          },
          // streaming plugins are configured normally under the plugins node
          plugins: {
              lighttpd: {
                  url: '../flowplayer.pseudostreaming-3.2.7.swf'
              }
          }
      });
     
        </script>

至此播放器支持了。

2、给flv视频加入关键帧和meta信息
上面提到,flv需要有meta信息才能正确地被分析并根据时间获取偏移量。有两个工具可以补全meta信息:

flvtool2:ruby写的一个著名工具,地址:https://rubyforge.org/projects/flvtool2/
jamdi:另外一个补全meta信息的工具,地址:http://yamdi.sourceforge.net/
两者都是命令行工具,也都有windows版本,下载了直接用就好。这里简单介绍一下用法:

flvtool2:

flvtool2 –U <input file> <output file>

/// <summary>  
       /// 处理flv实现添加matedata信息  
       /// </summary>  
       /// <param name="toolpath">toolpath="/FilesList/Exe/VideoTools/flvtool2.exe"</param>  
       /// <param name="filepath">视频地址</param>  
       public void flvtodrog(string toolpath, string filepath)  
       {  
           System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(toolpath);  
           startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;  
           startInfo.UseShellExecute = false;  
           startInfo.CreateNoWindow = false;  
           startInfo.Arguments = " -U " + filepath;  
           try  
           {  
               System.Diagnostics.Process.Start(startInfo);  
           }  
           catch (Exception e)  
           {  
               //  
           }  
       }  

jamdi:

jamdi –i <input file> –o <output file>

如此,meta信息就补全了。

3.在视频网站下新建一个handler,代码如下,使flv文件流输出

using System;
using System.IO;
using System.Web;
public class FLVStreaming : IHttpHandler
{
    private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");
    public FLVStreaming()
    {
    }
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            int pos;
            int length;
            // Check start parameter if present
            string filename = Path.GetFileName(context.Request.FilePath);
            using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string qs = context.Request.Params["start"];
                if (string.IsNullOrEmpty(qs))
                {
                    pos = 0;
                    length = Convert.ToInt32(fs.Length);
                }
                else
                {
                    pos = Convert.ToInt32(qs);
                    length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
                }
                // Add HTTP header stuff: 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", length.ToString());
                // Append FLV header when sending partial file
                if (pos > 0)
                {
                    context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
                    fs.Position = pos;
                }
                // Read buffer and write stream to the response stream
                const int buffersize = 16384;
                byte[] buffer = new byte[buffersize];
               
                int count = fs.Read(buffer, 0, buffersize);
                while (count > 0)
                {
                    if (context.Response.IsClientConnected)
                    {
                        context.Response.OutputStream.Write(buffer, 0, count);
      
      //Starts sending to client right away
      context.Response.Flush();
   
                        count = fs.Read(buffer, 0, buffersize);
                    }
                    else
                    {
                        count = -1;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }
    public bool IsReusable
    {
        get { return true; }
    }
    private static byte[] HexToByte(string hexString)
    {
        byte[] returnBytes = new byte[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        return returnBytes;
    }
}

在web.config中添加

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="FLVStreaming" path="*.flv" verb="*" type="FLVStreaming" resourceType="Unspecified" preCondition="integratedMode" />
        </handlers>
    </system.webServer>
</configuration>

还有一点别忘了,要在MIME类型添加.flv  flv-application/octet-stream

运行IIS服务管理器,右键点击默认Web站点,选择属性,移动到主目录选项页,并点击配置按钮。应用程序配置对话框弹出来了。点击添加按钮并在可执行字段输入aspnet_isapi.dll文件路径,在扩展字段输入.flv
ps:如果你的操作系统是XP SP2的话,在输入aspnet_isapi.dll路径时需要手工输入,不能用复制粘贴的形式,否则保存按钮变灰。浏览时去掉check file exit选项。


至此就大功告成了,现在,播放flv视频就可以任意拖动了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值