0

EDIT: Oh..... I lied! The commandline fails if the two source files have different bitrates (does samplerate matter?). Also, the source code below succeeds when the two sources are the same bitrate. So, this looks like a bitrate challenge now. Hrm....

Original question:

result.mp3 (from the commandline, below) is playable in WMP11.

The ASP.NET code below serves a file which plays fine in WMP11. But, when I uncomment those two lines, WMP11 won't play the file. Something about the code that merges the two MP3 files isn't to the satisfaction of WMP11.

How can I change the ASP.NET code to merge the two MP3s in the HTTP response with the success that the 'copy' commandline gives me?

protected void Page_Load(object sender, EventArgs e) {
    Response.Clear();
    Response.ContentType = "audio/mpeg";
    Response.AddHeader("Content-Disposition", "attachment; filename=test.mp3");
    var bytes1 = System.IO.File.ReadAllBytes(@"C:\test1.mp3");
    WriteBytesToResponse(bytes1);
    //var bytes2 = System.IO.File.ReadAllBytes(@"C:\test2.mp3");
    //WriteBytesToResponse(bytes2);
    Response.End();
}

private void WriteBytesToResponse(byte[] sourceBytes) {
    using (var sourceStream = new MemoryStream(sourceBytes, false)) {
        sourceStream.WriteTo(Response.OutputStream);
    }
}

copy /B test1.mp3+test2.mp3 result.mp3
lance
  • 16,092
  • 19
  • 77
  • 136
  • I'd shamelessly shell to the 'copy' commandline, but the real implementation of my problem needs to start streaming the first MP3 before the second MP3 is available (it's built dynamically, in a background thread, during the request). – lance Jan 19 '10 at 23:48
  • Also, the client isn't a browser, but a podcatcher. – lance Jan 19 '10 at 23:50
  • I have no control over the client code. It could be one of any number of podcatcher titles. – lance Jan 20 '10 at 00:04

2 Answers2

1

The answer to this question may be of help to you.

Basically, the response object won't concatenate the files properly, so you need to manually concatenate them then send the result to the client.

Community
  • 1
  • 1
Russ Bradberry
  • 10,705
  • 17
  • 69
  • 85
  • The referenced answer's "MP3 Frame headers" link was helpful. I've accepted that this definitely won't work if the MP3 is CBR, but I'm hoping to revisit this problem some time later with VBR files. – lance Jan 25 '10 at 19:39
0

I think the problem is the response object doesn't really know how to handle two files at once. When you attach a file to a web response it is working under the assumption that it's only going to be one file.

A better solution to provide both files at once would probably be to zip/tar them into one file and then send that as the attachment.

I don't even know if a browser would be able to properly handle two files in one response..likely not.

MadcapLaugher
  • 573
  • 2
  • 8
  • I don't want the client (a podcatcher -- see my added comment above) to see two files, for sure. I'm content to have it be entirely ignorant of the fact that the one byte stream it downloaded came from two merged sources. – lance Jan 19 '10 at 23:52
  • How much control do you have over the client code? If you were to create a tar file of your mp3s and send it over and have them extracted it would be easier to send as many as you like without needing to worry about response concatenation problems. – MadcapLaugher Jan 19 '10 at 23:58
  • I don't have any control over the client code. Also, I need to send the first MP3 when I don't yet have the second MP3 (which will get sent once it's ready -- in the same response). – lance Jan 20 '10 at 00:05