Stream MP3 file MVC3

前端 未结 1 947
梦谈多话
梦谈多话 2021-02-10 02:33

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

相关标签:
1条回答
  • 2021-02-10 03:05

    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>
    
    0 讨论(0)
提交回复
热议问题