Combine MP3 files in ASP.NET response?
EDIT: Oh..... I lied! The commandline fails if the two source files have different bitrates (does samplerate matter?). Also, the开发者_Python百科 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
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.
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.
精彩评论