Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two or more mp3 audio file in android?

I'm trying to merge mp3 audio files But not successful.

Here is my code.

public static void meargeAudio(List<File> filesToMearge)
{


    while (filesToMearge.size()!=1){

        try {
            FileInputStream fistream1 = new FileInputStream(new File(filesToMearge.get(0).getPath()));  //(/storage/emulated/0/Audio Notes/1455194356500.mp3) first source file
            FileInputStream fistream2 = new FileInputStream(new File(filesToMearge.get(1).getPath()));//second source file

            File file1 = new File(filesToMearge.get(0).getPath());
            boolean deleted = file1.delete();
            File file2 = new File(filesToMearge.get(1).getPath());
            boolean deleted1 = file2.delete();

            SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
            FileOutputStream fostream = new FileOutputStream(new File(filesToMearge.get(0).getPath()),true);//destinationfile

            int temp;

            while ((temp = sistream.read()) != -1) {
                // System.out.print( (char) temp ); // to print at DOS prompt
                fostream.write(temp);   // to write to file
            }

            filesToMearge.add(0,new File(filesToMearge.get(0).getPath()));
            filesToMearge.remove(1);
            filesToMearge.remove(1);


            fostream.close();
            sistream.close();
            fistream1.close();
            fistream2.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

e.g

firstFileSize =12kb

secondFileSize =10kb

finalfileSize=22kb

Size is accurate But sound is missing

No error but in result i found finalfile contains only first file audio second file audio is missing.

Don't know what is the issue.if any one know the solution help me.

Thanks

like image 356
Nisar Ahmad Avatar asked Feb 11 '16 12:02

Nisar Ahmad


People also ask

Can you combine multiple MP3 files?

Online MP3 Joiner If you have mp3 tracks that you want to consolidate, then you've come to the right place – here, you can turn your mp3 files into a single audio track that you can play on your phone, tablet, or your car's stereo! The web tool's interface is made to be simple and easy to navigate for your convenience.

How do I overlay two audio files on Android?

Choose a file and tap on Open. In the dialog “Mix files” you can set the audio level of the current file and the imported file.


2 Answers

I also struggled with that and solved it using mp4parser

import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.googlecode.mp4parser.authoring.tracks.AppendTrack;

For your case, I believe something like this should work:

public static void mergeAudio(List<File> filesToMerge) {

    String output = Environment.getExternalStorageDirectory().getAbsolutePath() + "output.mp3";

    while (filesToMerge.size()!=1){

        try {

            String[] videoUris = new String[]{
                filesToMerge.get(0).getPath(),
                filesToMerge.get(0).getPath()
            };

            List<Track> videoTracks = new LinkedList<Track>();
            List<Track> audioTracks = new LinkedList<Track>();

            for (Movie m : inMovies) {
                for (Track t : m.getTracks()) {
                    if (t.getHandler().equals("soun")) {
                        audioTracks.add(t);
                    }
                    if (t.getHandler().equals("vide")) {
                        videoTracks.add(t);
                    }
                }
            }

            Movie result = new Movie();

            if (!audioTracks.isEmpty()) {
                result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
            }
            if (!videoTracks.isEmpty()) {
                result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
            }

            Container out = new DefaultMp4Builder().build(result);

            FileChannel fc = new RandomAccessFile(output, "rw").getChannel();
            out.writeContainer(fc);
            fc.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

There are a few answers suggesting that, such as:

  • Merging 2 audio files sequentially in android?
  • Concatenate two audio files and play resulting file
like image 80
filipebarretto Avatar answered Nov 05 '22 04:11

filipebarretto


It is too late. But still, someone might need a proper solution. That is why I am suggesting to use AudioMixer-android library.

like image 2
ZeroOneZeroR Avatar answered Nov 05 '22 03:11

ZeroOneZeroR