我最近遇到了一个似乎无法解决的问题。我正在为一个播放音频文件(主要是.mp3和.wav)的应用程序创建一个简单的组件,并且在进行了一些测试之后,该应用程序似乎可以在Chrome中正常运行。
但是,在IE9中,这是我关心的唯一主要浏览器,在提供音频文件时,我似乎遇到了麻烦。
当前正在通过Controller Action请求文件,如下所示:
public ActionResult PlayAudioFile(string id)
{
AudioFile af = FileAgent.GetAudioFile(id);
try
{
//Grabs the file via a Provider
Stream resultStream = StorageProvider[af.Provider].ReadFile(af.Location);
resultStream.Position = 0;
//Added several headers in attempts to resolve the issues
Response.AppendHeader("Content-Disposition", "attachment; filename=audio.mp3");
Response.AppendHeader("Content-Length", resultStream.Length.ToString());
Response.AddHeader("Accept-Ranges", "bytes");
Response.Headers.Remove("Cache-Control");
Response.AddHeader("Content-Range","bytes 0-" + (resultStream.Length-1).ToString() + "/" + resultStream.Length.ToString());
//Returns the file and associated file type (from the Provider)
return new FileStreamResult(resultStream, resultStream.ContentType);
}
catch
{
//Uh oh. Error
}
}
上面的代码非常适合Chrome浏览器,并且可以在各种HTML5播放器中使用,例如 jPlayer , audio.js 和MediaElement。但是,当通过以下类似方式请求文件时(使用jPlayer语法):
$(this).jPlayer("setMedia", {
mp3: '@Url.Action("PlayAudioFile","Home")?id=D00023',
});
或audio.js语法:
player.load('@Url.Action("PlayAudioFile","Home")?id=D00023');
以及相关的audio.js音频标签:
<audio preload="auto" crossorigin="use-credentials"></audio>
这会触发适当的操作并返回FileStreamResult,但是似乎所有播放器都难以解密文件并正确读取文件。我尝试了几个玩家,并且在所有玩家中都遇到了同样的问题。
任何建议都将受到欢迎。谢谢。
注意:
$.ajax({ withCredentials: true})
和<audio crossorigin='use-credentials>
。两者都没有成功。 header 信息:
这两个请求(Chrome中的工作请求和IE9中的无效请求)都具有以下相同的 header :
Cache-Control: public, max-age=3600, s-maxage=0
Date: [Current Date Time]
Expires: [Current Date Time + max-age]
Vary: *
Content-Length: 77870
Content-Type: audio/mpeg
Last-Modified: [Date]
Accept-Ranges: bytes
Content-Range: bytes 0-77869/77870
Server: Microsoft-IIS/7.5
最佳答案
是。请勿将音频作为application/octet-stream
(仅指示通用内容),而是作为与您要提供的音频匹配的audio/mp3
,audio/wave
或some other appropriate contentType来提供。