【问题标题】:how to pass mp3 stream/response to view from controller?如何通过 mp3 流/响应从控制器查看?
【发布时间】:2014-01-30 01:49:07
【问题描述】:

我正在开发 asp.net 应用程序,我正在从我的视图中调用控制器操作。此视图需要将 mp3 返回到被调用函数。 Actionresult 的类型是什么。被调用的函数返回 mp3 响应。内存中api返回的mp3响应如何保存在内存中并返回查看?

这是我的控制器方法代码:

 public ActionResult getrecording()
        {
            byte[] file = new byte[100]; // Convert.ToByte("https://api.abc.com/api/v2/GetRecording?access_token=dfsdfsdfsdfsdfsdfsdfsdfsfsfdsf");


            //return (new FileStreamResult(fileStream, "audio/mpeg"));
            ////return (new FileContentResult(data, "audio/mpeg"));
        }

这是查看代码

 $("#jquery_jplayer_1").jPlayer({
                ready: function (event) {
                    $(this).jPlayer("setMedia", {
                          mp3: "/recording/getrecording" 
                    });
                },
                swfPath: "../../Content/JPlayer/js",
                supplied: "mp3",
                wmode: "window",
                size: 100,
                duration: "#duration"
            });

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    您可以使用 WebClient 下载远程文件,然后将其流式传输到响应:

    public ActionResult getrecording()
    {
        var client = new WebClient();
        var stream = client.OpenRead("https://api.abc.com/api/v2/GetRecording?access_token=dfsdfsdfsdfsdfsdfsdfsdfsfsfdsf");
        return File(stream, "audio/mpeg");
    }
    

    如果您希望提示客户端下载文件,只需添加一个名称:

    public ActionResult getrecording()
    {
        var client = new WebClient();
        var stream = client.OpenRead("https://api.abc.com/api/v2/GetRecording?access_token=dfsdfsdfsdfsdfsdfsdfsdfsfsfdsf");
        return File(stream, "audio/mpeg", "foo.mp3");
    }
    

    【讨论】:

    • 我不想下载它。我想将 mp3 响应传递给 jplayer jplayer.org/latest/developer-guide/#jPlayer-option-size
    • 在我添加的 jplayer 代码中,如果我这样做: $(this).jPlayer("setMedia", { mp3: "api.abc.com/api/v2/…" });它可以工作,但我需要传递不安全的访问密钥和令牌,因为用户可以看到它们。
    • 您应该将 jPlayer 的 url 指向您的控制器操作:mp3: '/somecontroller/getrecording'; 而不是远程服务。
    • 它可以工作,但在这种情况下,mp3 将加载到我的服务器上,并且我的流量很大,所以这将是我服务器的负担。
    • 流量现在确实会通过您的网络服务器,您将为带宽付费。届时,您将不得不在安全性和价格之间做出妥协。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多