2

Currently, I have a feature on an ASP.NET website where the user can play back MP3 Files. The code looks something like this:

Response.Clear();
Response.ContentType = "audio/mpeg";

foreach (DataChunk leChunk in db.Mp3Files.First(mp3 => mp3.Mp3ResourceId.Equals(id)).Data.Chunks.OrderBy(chunk => chunk.ChunkOrder))
{
    Response.BinaryWrite(leChunk.Data);
}

Unfortunately, if a larger MP3 file is selected, the audio does not begin to play until the entire file is downloaded, which can cause a noticeable delay. Is there any way to get the MP3 to start playing immediately, even though the entire file may not yet be transferred?

Phil K
  • 4,939
  • 6
  • 31
  • 56
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
  • This may help http://stackoverflow.com/questions/9600856/how-to-deliver-big-files-in-asp-net-response – cem Oct 20 '13 at 09:56

5 Answers5

1

You should be able to do what you want by writing to the outpstream of the response, i.e.:

Response.OutputStream.Write

It is also probably a good idea to check previously if Response.IsClientConnected and give up if not.

I found a demo that allows playback of mp3 files from an asp.net web application: http://aspsnippets.com/Articles/Save-MP3-Audio-Files-to-database-and-display-in-ASPNet-GridView-with-Play-and-Download-option.aspx

Rui
  • 4,847
  • 3
  • 29
  • 35
1

try this:

    Response.BufferOutput = false; //sets chunked encoding
    Response.ContentType = "audio/mpeg";

    using (var bw = new BinaryWriter(Response.OutputStream))
    {
        foreach (DataChunk leChunk in db.Mp3Files.First(mp3 => mp3.Mp3ResourceId.Equals(id)).Data.Chunks.OrderBy(chunk => chunk.ChunkOrder))
        {
            if (Response.IsClientConnected) //avoids the host closed the connection exception
            {
                bw.Write(leChunk.Data); 
            }
        }
    }

Also, go yo your web.config file and do this if you still have problems with chunked encoding:

<system.webServer>
  <asp enableChunkedEncoding="true" />
</system.webServer>

The error you reported above about the host being closing the connection is happening probably because you are opening the page using the browser and when the browser reads the content type, it opens the media player and closes itself who had the opened connection which was then closed, causing that error, so to avoid this, you need to check periodically whether your client is still connected or not.

Finally, I would use a Generic Handler (.ashx) or a custom handler and set a .mp3 extension for this if you are using a aspx page to avoid the unnecessary overhead of the web page.

I hope this helps.

Bruno
  • 4,337
  • 12
  • 42
  • 55
0

Try setting Response.BufferOutput = false before streaming the response.

Oran Dennison
  • 3,237
  • 1
  • 29
  • 37
  • Setting that value to false results in "The remote host closed the connection. The error code is 0x800703E3." – GWLlosa Oct 20 '13 at 00:29
0

If the location of the MP3 files are publicly available to your user then an alternative approach could be to just return the MP3's URL and use the HTML 5 audio tags in your mark up to stream the music. I am pretty sure that the default behaviour of the audio tag would be to stream the file rather than wait until the whole file has downloaded.

CeejeeB
  • 3,034
  • 4
  • 25
  • 32
0

One method to support this would be implementing HTTP byte range requests.

By default I don't believe that ASP.NET does this, and definitely won't if using any of the code in the questions or the answer.

You can implement this manually with a little work though. Another option, which would be much less dev work, would be to let IIS serve a static file. I assume that isn't an option though.

Here's an example implementation: http://www.codeproject.com/Articles/820146/HTTP-Partial-Content-In-ASP-NET-Web-API-Video

Ian Newson
  • 7,679
  • 2
  • 47
  • 80