Search code examples
androidaudiomergeinputstreamogg

Android: How merge(join) two OGG music files to one?


How can I merge two OGG files to one in Android (4.0+)? So far I searched a lot before posting here but I didn´t find anything useful about this. I tried to merge two OGG file with the code below:

        File newFile = new File(Environment.getExternalStorageDirectory(), "test.ogg");

        Vector<InputStream> inputStreams = new Vector<InputStream>();
        for (String path : Globals.mCurrentProject.getSamples()) {

            inputStreams.add(new FileInputStream(path));
        }

        Enumeration<InputStream> enu = inputStreams.elements();
        SequenceInputStream sis = new SequenceInputStream(enu);

        FileOutputStream fos = new FileOutputStream(newFile);

        byte[] buf = new byte[8 * 1024]; // or 4 * 1024

        while (true) {
            int r = sis.read(buf);
            if (r == -1) {
                break;
            }
            fos.write(buf, 0, r);
        }

        sis.close();
        fos.close();

But it´s not ideal code. This example merges the files but it´s not set the right track duration and there is a problem with song playing etc. (it should set song metadata: duration,...)

Is it possible somehow (in simple way) join two (or more) OGG files in Android?

In Java is audioInputStream but it´s not in Android. What solution do you recommend for this problem? Maybe join files on the server (send some request and in answer get file)? I prefer do this in my app but if it will be a problem, maybe use server or something else.

Edit:

Can MediaCodec or MediaMuxer help with it?


Solution

  • Solution:

    1) Merge Ogg files using the SequenceInputStream (like above)

    2) Using FFMPEG Library for Android you to convert ogg to mp3

    3) Everything looks good and works. But it´s a little slow :-)