1

I'd like to merge two recorded audio files. Well, so far i searched a lot before posting here. Suppose if i merge first.mp3 and second.mp3 into merged.mp3 then merged.mp3 doesn't have data of second.mp3 It is same as first.mp3.

Here is my code snippets

MainActivity Snippets

Handler handler=new Handler();
                  handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        File first =  new File(path1);
                        File second = new File(path2);
                        String filepath = Environment.getExternalStorageDirectory().getPath();
                        File file = new File(filepath, "Dictatapp");
                        mergeSongs(new File(file.getAbsoluteFile()+"/mergedFile.mp3"), new File[] {first, second});

                    }
                },2000);

Merge Function

private void mergeSongs(File mergedFile,File...mp3Files){

    try {
        FileInputStream fis1 = new FileInputStream(mp3Files[0]);
        FileInputStream fis2 = new FileInputStream(mp3Files[1]);
        Log.i("merge",mp3Files[0]+" "+mp3Files[1]);
        Toast.makeText(MainActivity.this, mp3Files[0].toString()+mp3Files[1].toString(), Toast.LENGTH_LONG).show();
        SequenceInputStream sis = new SequenceInputStream(fis1,fis2);

        if(!mergedFile.exists()){
            mergedFile.createNewFile();}

        FileOutputStream fos = new FileOutputStream(mergedFile);

        int temp;

         while ((temp = sis.read())!= -1){

                fos.write((byte)temp);

            }

         fis1.close();
         fis2.close();
         sis.close();
         fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

}
user3404836
  • 11
  • 1
  • 3
  • your code seems to fine. Just ensure that path1 and path2 are not same. – Ashwin N Bhanushali May 28 '14 at 09:17
  • Yeah.. they are not same!! :/ – user3404836 May 28 '14 at 09:53
  • How you came to conclusion that data is contained in merged mp3 is same as first.mp3? Please have a look at size of first.mp3,second.mp3 and merged.mp3.if sizeOf(merged.mp3)==sizeOf(first.mp3)+sizeOf(second.mp3) then I think you need to ffmpeg(https://github.com/guardianproject/android-ffmpeg) library to concatenate the two mp3 files. since music files have different file headers. since It's not simple as concatenating two text files. – Ashwin N Bhanushali May 28 '14 at 10:01
  • Size of (first.mp3) and (merged.mp3) is different but lenght of audio is same thats why i thought so. Ffmpeg concern, I have implemented the same using Ffmpeg before. It's also working fine when i merge two songs of mp3 format. However, when i merge two recorded audio of mp3 format then resulting merged file can't played. – user3404836 May 28 '14 at 10:22
  • Then I think problem is with recorded audio format. Try to make sure that recorded audio is created with proper mp3 format. – Ashwin N Bhanushali May 28 '14 at 10:24
  • I am able to play recorded audio file so file format might not be an issue i guess!! – user3404836 May 28 '14 at 10:29
  • i got same problem if you found solution please in form me – bhavesh kaila Oct 13 '14 at 12:11

0 Answers0