使用IISHandler对无线电进行流式传输

本文关键字:传输 IISHandler 无线电 使用 | 更新日期: 2023-09-27 18:26:18

我正试图在我的网站上播放我自己的在线广播。我现在专注于简单地使用http处理程序和html5来实现这一点

如何保持IISHandler不断运行,而不会为每个用户结束?就像一个真正的在线广播。

使用IISHandler对无线电进行流式传输

您可以将redirect直接handler中的任何url,前提是该url具有audio内容,您打算将handler放置到<audio> src。所以你可以试试这个。

public void ProcessRequest(HttpContext context)
{
    //context.Response.Redirect("http://api.soundcloud.com/tracks/148976759/stream?client_id=201b55a1a16e7c0a122d112590b32e4a");
    // you can use above or else below
    byte[] content = null;
    string fileName = context.Server.MapPath(@"'mp3'") + context.Request["file_name"];
    if (File.Exists(fileName))
    {
        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            content = new byte[System.Convert.ToInt32(stream.Length)];
            stream.Read(content, 0, System.Convert.ToInt32(stream.Length));
            context.Response.ContentType = "audio/mp3";
            context.Response.OutputStream.Write(content, 0, content.Length);
        }
    }
}

<audio src="Handler1.ashx" controls></audio>

关于IISHandler constantly running?——当您在IIS中托管它时,任何对处理程序的请求(即,您将有一个具有音频src=handlerhtml页面,因此最终用户点击该html页面,处理程序将被调用),它都将处理该请求。类似于aspx的概念。它们托管在IIS中,任何对此的请求都将被处理。你明白我的意思了吗?