views:

41

answers:

1

test1.mp3 and test2.mp3 have the same bitrate and sample rate, and I'm trying to merge them in an HTTP response. The resulting file is test.mp3.

test.mp3 plays fine in WMP12 and VLC. In WMP11, I hear only the audio which came from test1.mp3. At the moment you expect to hear the beginning of test2.mp3's audio, the player stops playing. WMP11 reports no errors... it just stops playing.

What needs to change such that test.mp3 will play correctly in WMP11?

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);
    }
}
A: 

MP3 files contain a full MPEG1 header, amongst other information, such as the stream length. You simply cannot concatenate them. WMP read the header, determined the playback length, and stops when the first file is done. The rest of the data is ignored as it shouldn't be there.

You will need to use some library or utility which understands MPEG1 or MP3 files to do the concatenation.

Yann Ramin
I need to send the first file's audio over the HTTP response before the second file's audio has even been created (it's created dynamically during the response). I can't control the timeout tolerance of the client (any of a number of podcatchers), and the first audio file is sent immediately, to prevent that timeout that _will_ occur if I don't send a single byte until the second audio is created dynamically (this takes several minutes). Sounds like I'm between a rock and a hard place?
lance
@lance: If you can't start delivering over an HTTP socket relatively quickly, you should probably rethink your request to file generation strategy - its also asking for a host of other problems (DoS, et)
Yann Ramin
@theatrus: At long last, I concur, really. The last few days have exposed other flaws in the project's design, too. This was never to be for the masses, and I learned a lot writing the code that is now "useless". I'll likely soon mark your response as the answer. You were very helpful. Thank you.
lance