流MP3文件MVC3 [英] Stream MP3 file MVC3
问题描述
我试图使用流音频HTML5标记的文件。我已经把控制器动作返回一个FileStream,并连接到src的音频。但是,内容不加载到音频标签,当我preSS默认的播放按钮不玩了。我知道当我直接访问SRC控制器正常工作。但是,它不会在HTML5音频标签的工作。
I am trying to stream a file using the audio HTML5 tag. I have put the Controller action to return a FileStream and attached it to the src for the audio. However, the content is not loading into the audio tag and doesn't play when I press the default play button. I know the controller is working when I access the src directly. However, it doesn't work in the HTML5 audio tag.
谁能告诉我,我缺少的是什么?
Can anyone tell me what I am missing?
推荐答案
您不应该返回一个的FileStream
,你应该返回一个 FileStreamResult
或 FilePathResult
从你的控制器动作,这样的:
You should not return a FileStream
, you should return a FileStreamResult
or a FilePathResult
from your controller action, like this:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult MyAudio()
{
var file = Server.MapPath("~/app_data/test.mp3");
return File(file, "audio/mp3");
}
}
和〜/查看/主页/ Index.cshtml
查看:
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sound Sample</title>
</head>
<body>
<article class="audio">
<header>
<h2>Some audio</h2>
</header>
<audio controls>
<source src="@Url.Action("myaudio")" type="audio/mp3" />
<p>Your browser does not support HTML 5 audio element</p>
</audio>
</article>
</body>
</html>
这篇关于流MP3文件MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!