Rio*_*ams 4 .net c# audio html5 asp.net-mvc-3
我最近遇到了一个我似乎无法解决的问题.我正在为播放音频文件(主要是.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
}
}
Run Code Online (Sandbox Code Playgroud)
以上内容适用于Chrome,适用于各种HTML5播放器,如jPlayer,audio.js和MediaElement.但是,当通过以下内容请求文件时(使用jPlayer语法):
$(this).jPlayer("setMedia", {
mp3: '@Url.Action("PlayAudioFile","Home")?id=D00023',
});
Run Code Online (Sandbox Code Playgroud)
或audio.js语法:
player.load('@Url.Action("PlayAudioFile","Home")?id=D00023');
Run Code Online (Sandbox Code Playgroud)
和相关的audio.js音频标签:
<audio preload="auto" crossorigin="use-credentials"></audio>
Run Code Online (Sandbox Code Playgroud)
这会触发正确的操作并返回FileStreamResult,但是看起来所有玩家都难以解密文件并正确读取它.我已经尝试了几个玩家并在所有玩家中遇到过同样的问题.
任何建议都会受到欢迎.谢谢.
笔记:
正如其中一条评论中所述,我尝试使用多种和多种评论类型来解决此问题.其中没有一个有效.
在探索这个问题的一些可能的根本原因时,我注意到删除[Authorize]属性清除了直接访问文件的一些问题,但所有玩家都无法在IE9中加载该文件.
我现在确信这可能是凭据问题,当我需要通过jPlayer或audio.js(或者你有什么)请求文件时,我需要包含必要的凭据以允许它通过控制器访问文件.
试图使用$.ajax({ withCredentials: true})和<audio crossorigin='use-credentials>.两者都没有成功.
标题信息:
这两个请求(Chrome中的工作和IE9中的非功能请求)都具有相同的标头:
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
Run Code Online (Sandbox Code Playgroud)