Search code examples
c#windows-phone-8mergemp3

How to merge MP3 files in WP8 (edit)


I have list of MP3 files that I would like to merge them into one file. I downloaded files locally into isolated storage, but I have no idea how to merge them. Google doesn't help either. I don't know if its possible in WP8. (2) If not possible what specific solution you could advice (I also have my files in web).

I write below code for merging, but this always writes the last file.

string[] files = new string[] { "001000.mp3", "001001.mp3", "001002.mp3", "001003.mp3", "001004.mp3", "001005.mp3", "001006.mp3", "001007.mp3" };

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
    string newFileName = "001.mp3";
    foreach (string _fileName in files)
    {
        if (storage.FileExists(_fileName))
        {
            byte[] bytes = new byte[1];
            using (var f = new IsolatedStorageFileStream(_fileName, FileMode.Open, storage))
            {
                bytes = new byte[f.Length];
                int byteCount;
                using (var isfs = new IsolatedStorageFileStream(newFileName, FileMode.OpenOrCreate, storage))
                {
                    while ((byteCount = f.Read(bytes, 0, bytes.Length)) > 0)
                    {
                        isfs.Write(bytes, 0, byteCount);
                        isfs.Flush();
                    }
                }
            }
        }
    }
}

How can i add (merge) files to end of the newly created file?


Solution

  • I added isfs.Seek(0, SeekOrigin.End); before writing to file. This is how we can simply merge same bitrate mp3 files.

    the code looks like below:

    using (var isfs = new IsolatedStorageFileStream(newFileName, FileMode.OpenOrCreate, storage))
    {
        isfs.Seek(0, SeekOrigin.End); //THIS LINE DID SOLVE MY PROBLEM
        while ((byteCount = f.Read(bytes, 0, bytes.Length)) > 0)
        {
            isfs.Write(bytes, 0, byteCount);
            isfs.Flush();
        }
    }
    

    Please improve my code if it seems to be longer.