Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为<;音频>;?_C#_Asp.net_Html_Cross Browser_Html5 Audio - Fatal编程技术网

C# 如何为<;音频>;?

C# 如何为<;音频>;?,c#,asp.net,html,cross-browser,html5-audio,C#,Asp.net,Html,Cross Browser,Html5 Audio,我正在为HTML5 audio.aspx页面提供二进制数据。该文件可以播放,但在Safari(iPad)、Chrome和Firefox中SeekBar和重播存在问题。 编辑我简化了代码(希望不会太多),并提供了完整的列表(页面文件名显然是play.aspx) 受保护的无效页面加载(对象发送方、事件参数e) { if(请求[“文件名”]!=null) { 字符串FilePath=MapPath(请求[“文件名]); long fSize=(new System.IO.FileInfo(FileP

我正在为HTML5 audio.aspx页面提供二进制数据。该文件可以播放,但在Safari(iPad)、Chrome和Firefox中SeekBar和重播存在问题。
编辑我简化了代码(希望不会太多),并提供了完整的列表(页面文件名显然是play.aspx)


受保护的无效页面加载(对象发送方、事件参数e)
{
if(请求[“文件名”]!=null)
{
字符串FilePath=MapPath(请求[“文件名]);
long fSize=(new System.IO.FileInfo(FilePath)).Length;
//标题
AddHeader(“内容长度”,fSize.ToString());
Response.ContentType=“音频/mp3”;
//资料
Response.WriteFile(文件路径);
Response.End();
};      
}
直接的


简单回答:需要处理浏览器
范围请求

完整故事

在比较Firefox和IIS之间捕获的通信时,我注意到直接链接的响应状态为
206部分内容
,而间接链接的响应状态为
200 OK

对请求进行更仔细的分析表明,Firefox请求
范围:bytes=0-
和iPad
范围:bytes=0-1
以及以后的
范围:bytes=0-fullsize
()
设置
Response.StatusCode=206
足以愚弄Firefox,但不能愚弄Safari。但我知道关键信息,其余的都很简单。有必要遵守
范围
请求请参见:SO:。我在中找到了如何做的解释。因此,新代码为(仅显示活动部分,其余部分与答案相同):

添加其他标题(
Content Disposition
Accept Ranges
Access Control Allow Origin
)可能很方便,但其目的是提供尽可能简单的解决方案


吸取的教训:如果我没有被固定到
,并且也查找了
,我会更快地找到解决方案。

你应该看看jPlayer,它为你内置了所有功能,对于不支持HTML5@MattyMo的浏览器,它有后备功能:jPlayer很好。然而,我认为在现在的浏览器上,我将面临HTML5音频的同样问题(源数据未被控件正确接受)。我有一种无法使用公共url访问文件的情况。您提供的示例非常有效。但它迫使音频控制器接管整个浏览器窗口。如果我想在现有页面中提供一个音频控制器,同时提供自定义布局和显示元数据,该怎么办?@JakobLithner:解决方案简化为一个页面。理解的关键可能是src=“play.aspx?filename=sound.mp3”。因此,您可以使用不同的页面来提供数据(包含呈现的页面加载)和控件所在的页面。非常感谢!该解决方案对Safari MacOS、FF和Chrome非常有效。
<%@ Page Language="C#"  %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
       if ( Request["filename"] != null)
       {
           string FilePath = MapPath(Request["filename"]);
           long fSize = (new System.IO.FileInfo(FilePath)).Length;
           //Headers
           Response.AddHeader("Content-Length",fSize.ToString());
           Response.ContentType = "audio/mp3";
           //Data
           Response.WriteFile(FilePath);
           Response.End();   
       };      
    }
</script>
<body>
    <p>Direct</p>
       <audio controls="controls" preload="none">
         <source src="sound.mp3" type="audio/mp3" />
      </audio>
      <p>Provided</p>
      <audio controls="controls" preload="none">
         <source src="play.aspx?filename=sound.mp3" type="audio/mp3" />
      </audio>
</body>
</html>
    protected void Page_Load(object sender, EventArgs e)
    {
       if ( Request["filename"] != null)
       {
           string FilePath = MapPath(Request["filename"]);
           long fSize = (new System.IO.FileInfo(FilePath)).Length;
           long startbyte = 0;
           long endbyte=fSize-1;
           int statusCode =200;
           if ((Request.Headers["Range"] != null))
           {
               //Get the actual byte range from the range header string, and set the starting byte.
               string[] range = Request.Headers["Range"].Split(new char[] { '=','-'});
               startbyte = Convert.ToInt64(range[1]);
               if (range.Length >2 && range[2]!="")  endbyte =  Convert.ToInt64(range[2]);
               //If the start byte is not equal to zero, that means the user is requesting partial content.
               if (startbyte != 0 || endbyte != fSize - 1 || range.Length > 2 && range[2] == "")
               {statusCode = 206;}//Set the status code of the response to 206 (Partial Content) and add a content range header.                                    
           }
           long desSize = endbyte - startbyte + 1;
           //Headers
           Response.StatusCode = statusCode;
           Response.ContentType = "audio/mp3";
           Response.AddHeader("Content-Length",desSize.ToString()); 
           Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte , fSize));
           //Data
           Response.WriteFile(FilePath,startbyte,desSize);
           Response.End(); 
       };      
    }