views:

283

answers:

3

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
A: 

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
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
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
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
+1  A: 

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.

Russ Bradberry
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
A: 

Hi, I tried the codes but got error: 'System.IO.Stream' does not contain a definition for 'WriteTo'

Anyone know why that happened?

Gakki