2
Reply

mp3 streaming with httphandler

Richard

Richard

Mar 9 2011 2:43 PM
5.4k
Trying to stream an mp3 in .net 3.5. I'm using a free flash swf to play the mp3 and am putting the handler url in the params in the flash object. In firefox 3.6, it just sits there and doesn't load or play the mp3. Loads and automatically plays in IE8. If I hit the handler url in firefox directly, it downloads the mp3. I'm using a handler so I can keep people from downloading the mp3 (which I realize contradicts the previous sentence, but I disabled the anti-download code to see if the handler works at all in firefox). Any ideas?

html:

<object
    type="application/x-shockwave-flash"
    data="zplayer.swf?mp3=Player.ashx?Title=MySong&c1=333333&vol=100&autoplay=1"
    width="200" height="20" />
    <param name="movie" value="zplayer.swf?mp3=Player.ashx?Title=MySong&c1=333333&vol=100&autoplay=1" />
    <embed src="zplayer.swf?mp3=Player.ashx?Title=MySong&c1=333333&vol=100&autoplay=1" quality="high" bgcolor="#e6e6e6" width="400" height="15" name="xspf_player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>


handler:

  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  public class Player1 : IHttpHandler
  {

      public void ProcessRequest(HttpContext context)
      {
          if (context.Request.QueryString["Title"] != string.Empty)
          {
              string filename = context.Request.QueryString["Title"] + ".mp3";
              string path = context.Server.MapPath("audio");
              string file = path + "\\" + filename;
              if (System.IO.File.Exists(file))
              {
                  FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
                  context.Response.Buffer = true;
                  context.Response.Clear();
                  context.Response.ClearContent();
                  context.Response.ClearHeaders();
                  context.Response.AddHeader("content-disposition", "attachement; filename=" + filename);
                  context.Response.AddHeader("content-length", fs.Length.ToString());
                  context.Response.ContentType = "audio/mpeg";
                  byte[] bytes = new byte[fs.Length];
                  int bytesToRead = (int)fs.Length;
                  int bytesRead = 0;
                  while (bytesToRead > 0)
                  {
                      int n = fs.Read(bytes, bytesRead, bytesToRead);
                      if (n == 0) break;
                      bytesRead += n;
                      bytesToRead -= n;
                  }
                  bytesToRead = bytes.Length;
                  context.Response.OutputStream.Write(bytes, 0, bytes.Length);
              }
          }
          context.Response.End();
      }

      public bool IsReusable
      {
          get
          {
              return false;
          }
      }
  }


Answers (2)