Merging 2 audio files sequentially in android?

2020-06-23 06:42发布

how can merge two audio files to a single file in android platform. that is if i have two audio files namely A and B.Please give me the code to create a new audio file C that is appended by B after A. Is there any need to convert the audio files to byte/short array ??...

i did like this but it plays 1st file only.

            File firstFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict136.3gp");

            File secondFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict139.3gp");

            System.out.println("1 file:::::"+firstFile+"\n 2nd file::"+secondFile);


           FileInputStream fistream1;
        try {
            fistream1 = new FileInputStream(firstFile);
            FileInputStream fistream2 = new FileInputStream(secondFile);//second source file



            SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
            FileOutputStream fostream = new FileOutputStream("/mnt/sdcard/Android/data/Dictation/Audio/merged.3gp");//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
            }



            filePath="/mnt/sdcard/Android/data/Dictation/Audio/merged.3gp";
            playAudio(filePath);


            fostream.close();
            fistream1.close();
            fistream2.close();

thanks in advance.

标签: java android
2条回答
▲ chillily
2楼-- · 2020-06-23 07:06

To combine audio files that are in an 3gp or mp4 container you should be able to use https://github.com/sannies/mp4parser Check out the samples: https://github.com/sannies/mp4parser/blob/master/examples/src/main/java/com/googlecode/mp4parser/AppendExample.java

get the mp4parser jar (via maventral) and do this:

        File firstFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict136.3gp");
        File secondFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict139.3gp");

        Movie movieA = MovieCreator.build(firstFile);
        Movie movieB = MovieCreator.build(secondFile);
        final Movie finalMovie = new Movie();

        List<Track> audioTracks = new ArrayList<>();
        for (Movie movie : new Movie[] { movieA, movieB}) {
            for (Track track : movie.getTracks()) {
                if (track.getHandler().equals("soun")) {
                    audioTracks.add(track);
                }
            }
        }
        finalMovie.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));

        final Container container = new DefaultMp4Builder().build(finalMovie);
        File mergedFile = new File("/mnt/sdcard/Android/data/Dictation/Audio/merged.3gp");
        final FileOutputStream fos = new FileOutputStream(mergedFile);
        FileChannel fc = new RandomAccessFile(mergedFile, "rw").getChannel();
        container.writeContainer(fc);
        fc.close();
查看更多
Rolldiameter
3楼-- · 2020-06-23 07:07

You can try using File Input/output Stream for this. To read any file in byte arrays use

byte[] buffer = new byte[FILE_SIZE_HERE];
FileInputStream fs = new FileInputStream(file);
int bytesRead = 0;
bytesRead = fs.read(buffer, 0, FILE_SIZE_HERE);

Here the buffer contain you file's byte array. In this way you can get the byte array of both the file. now in 2nd step when you have taken the byte array of bot file you need to copy both of byte array into one single array and then you can write this (3rd new array) to a new file using this

FileOutputStream fout = new FileOutputStream(tempFile, true);
fout.write(data);
fout.close();

Where data is the new 3rd array you will get after merging both arrays

EDIT: See this how to combine two arrays

Hope this will help you.

查看更多
登录 后发表回答