Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass mp3 stream/response to view from controller?

I am working on asp.net application and I am making a call to controller action from my view. this view need to return mp3 to called function. What will be type of Actionresult. The function which is being called returns mp3 response. How to hold mp3 response in memory returned by api in memory and return to view ?

here is my controller method code:

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

and here is the view code

 $("#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"
            });
like image 332
DotnetSparrow Avatar asked Oct 21 '25 06:10

DotnetSparrow


1 Answers

You could use a WebClient to download the remote file and then stream it to the response:

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

and if you want the client to be prompted to download the file simply add a name:

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");
}
like image 51
Darin Dimitrov Avatar answered Oct 22 '25 21:10

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!