public Blog<BenR>

Ben Reichelt's Blog

<December 2008>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910


Navigation

Blogs

Subscriptions

Post Categories



Sunday, January 02, 2005 - Posts

Can't stream mp3 file in .net

I am trying to convert an internal application that we have that streams mp3 files to winamp.  The app is currently written in asp, and I'm moving it to asp.net.  I copied the streaming code almost exactly from the vbscript to c#, but now when I try to listen to an mp3 from the new site, I get the great error message "error syncing to mpeg."  Does anyone have experience in streaming mp3 files to winamp?  The asp code reads the file from an ADODB.Stream object in 2048 size chunks and buffers the content.  I initially did the same thing in c#, but after getting the error, I tried simplifying the code and just using the Response.WriteFile(string path) method.  The reason for reading the file in chunks is so that large files don't timeout.

I think that it might have something to do with a bad header or something, but I don't know if asp.net puts in extra headers automatically, in any case, I don't see why these don't work the same way.  I can load both examples in a firefox browser window, and the content is the same, its the base64 content of the file, so I know that its actually reading the correct data, winamp just won't recognize the file as an mp3.

asp code (this works):

Response.Buffer = True
Response.Clear
Response.AddHeader "Content-Disposition", "inline; filename=" & FileName
Response.AddHeader "Content-Length", FileSize

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile FilePath

lSize = objStream.size

CHUNK=2048
NumberofBlocks=(lSize -(lsize mod chunk))/chunk
For lBlocks = 1 To NumberofBlocks
If Response.IsClientConnected = False Then Exit For
Response.BinaryWrite objStream.Read(CHUNK)
Response.Flush
Next

objStream.Close
Set objStream = Nothing


c# code:

HttpResponse Response = System.Web.HttpContext.Current.Response;

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader(
"Content-Disposition", "inline; filename=" + System.Web.HttpContext
.Current.Server.UrlEncode(SongName));
Response.AppendHeader(
"Content-Length"
, Size.ToString());
Response.WriteFile(path);

/*

System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
int CHUNK = 2048;
byte[] buffer = new byte[CHUNK];

while (fs.Read(buffer, 0, CHUNK) > 0)
{
      if (!Response.IsClientConnected)
               break;

     Response.OutputStream.Write(buffer, 0, buffer.Length);
     Response.Flush();
}

fs.Close();
fs = null;

*/

Update: I got it working, there was something in my base page class that was goofing the stream up somehow. I think it had to do with cookies, but I will investigate further.

posted Sunday, January 02, 2005 11:23 AM by reic0113




Powered by Dot Net Junkies, by Telligent Systems