Stream MP3 file MVC3

早过忘川 提交于 2019-12-03 07:57:47

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");
    }
}

and the ~/Views/Home/Index.cshtml view:

@{
    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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!