Search code examples
javaandroidjoinmp3output

Join two MP3s in Android


I saw a few pages about it, but I found the solution to the problem, would like to join two mp3 tracks into one, but at the time he writes the output (juntos4.mp3, it is just the songs, someone can help me?

"My" Code...

                FileInputStream fis1 = new FileInputStream("/sdcard/1408586436107.mp3");  // first source file
                FileInputStream fis2 = new FileInputStream("/sdcard/1408586281745.mp3");//second source file
                SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
                FileOutputStream fos = new FileOutputStream("/sdcard/juntos4.mp3");//destinationfile

                int temp;

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

                        fos.write(temp);

                    }

                    fis1.close();
                    fis2.close();
                    sis.close();
                    fos.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

Solution

  • create the FILE not the directory!

    newFile.createNewFile();
    
    
    FileInputStream fistream1 = new FileInputStream(newFile1 );  // first source file
        FileInputStream fistream2= new FileInputStream(newFile2 );//second source file
        Vector<FileInputStream> v = new Vector<FileInputStream>();
        v.add(fistream1);
        v.add(fistream2);
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
    
        if(!newFile.exists()){
            newFile.createNewFile();
            FileOutputStream fostream=new FileOutputStream(newFile, true);
            int temp;
    
            while( ( temp = sistream.read() ) != -1)
            {
                System.out.print( (char) temp ); // to print at DOS prompt
                fostream.write((byte)temp);   // to write to file
            }
    
            fostream.close();
            sistream.close();
            fistream1.close();
            fistream2.close();
        }