1

I have this code to read bytes to another file. But I'm not able to concatenate two mp3 files into one. Am I missing something?

public static void main(String[] args) {
  String strFileName = ("D:/Music/Assb/Love.mp3");
                BufferedOutputStream bos = null;

                try
                {
                        //create an object of FileOutputStream
                        FileOutputStream fos = new FileOutputStream(new File(strFileName));

                        //create an object of BufferedOutputStream
                        bos = new BufferedOutputStream(fos);

                        String str = "D:/Music/Assembled/Heart001.mp3" 
                            + "D:/Music/Assembled/Heart002.mp3";

                        /*
                         * To write byte array to file use,
                         * public void write(byte[] b) method of BufferedOutputStream
                         * class.
                         */
                         System.out.println("Writing byte array to file");

                         bos.write(str.getBytes());

                        System.out.println("File written");
Edu
  • 2,017
  • 17
  • 23
user1573066
  • 17
  • 1
  • 3
  • Do you simply want to concatenate the files into one big file or do you want to 'mix' them so one file plays 2 songs ? – bart s Aug 04 '12 at 08:07
  • I want to concatenate into one big file. Is it possible? – user1573066 Aug 04 '12 at 08:09
  • your `str` does not contain a legal filename. – bart s Aug 04 '12 at 08:11
  • sorry, but what do you mean by legal? – user1573066 Aug 04 '12 at 08:15
  • 2
    A mp3 file is not the same as a simple text file. It is compressed, consists of frames, which itself have headers. In order to join two mp3 files, you may have do first decompress, join, and then compress it again, since the compression isn't equal or the frames contain data that is not compatible with each other, see [mp3 file specification](http://en.wikipedia.org/wiki/MP3#File_structure). – Manuel Leuenberger Aug 04 '12 at 08:16
  • I meant "invalid". Your str = `D:/Music/Assembled/Heart001.mp3D:/Music/Assembled/Heart002.mp3` which is not an valid filenam. BTW is this for java? – bart s Aug 04 '12 at 08:17

4 Answers4

2

It`s suck. Mp3 file starts with headers. For correct merging you have to skip first 32 bytes. Try this.

 try {
            FileInputStream fistream1 = new FileInputStream(_file_name);
            File f = new File(new File(_file_name).getParent()+"/final.mp3");
            if(!f.exists())
            {
                f.createNewFile();
            }
            FileOutputStream sistream = new FileOutputStream((new File(_file_name)).getParent()+"/final.mp3");
            int temp;
            int size = 0;
            temp = fistream1.read();
            while( temp != -1)
            {
                sistream.write(temp);
                temp = fistream1.read();
            };
            fistream1.close();
            FileInputStream fistream2 = new FileInputStream(temp_file);
            fistream2.read(new byte[32],0,32);
            temp = fistream2.read();
            while( temp != -1)
            {
                sistream.write(temp);
                temp = fistream2.read();
            };
            fistream2.close();
            sistream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
0

You need to do this in two steps

String str = "D:/Music/Assembled/Heart001.mp3";

>>> ADD code to open the file given by str <<<<
bos.write(strFile.getBytes());
>>> Add code to close the file


str = "D:/Music/Assembled/Heart002.mp3";
>>> ADD code to open the file given by str <<<<
bos.write(strFile.getBytes());
>>> Add code to close the file

And as you can see you need code to open the mp3 file to read it

bart s
  • 5,068
  • 1
  • 34
  • 55
-1

What Are You Trying For...Actually..if You Want To Read 2 Files to Byte Stream the dont String str = "D:/Music/Assembled/Heart001.mp3" + "D:/Music/Assembled/Heart002.mp3"; make str1=D:/Music/Assembled/Heart001.mp3 and str2=D:/Music/Assembled/Heart002.mp3 and read str1,str2 seperately through bufferedoutputsream

J.K
  • 2,290
  • 1
  • 18
  • 29
  • it has the same results. i need it to combine the data of both mp3 files and be able to play after it's combined – user1573066 Aug 04 '12 at 08:58
-1

This code will work well and merge audio of similar type with in seconds...

try {


                   InputStream in = new FileInputStream("C:\\a.mp3");//firstmp3
                    byte[] buffer = new byte[1 << 20];  // loads 1 MB of the file
                    OutputStream os = new FileOutputStream(new File("C:\\output.mp3", true);//output mp3
                    int count;
                    while ((count = in.read(buffer)) != -1) {
                        os.write(buffer, 0, count);
                        os.flush();
                    }
                    in.close();
                    in = new FileInputStream("C:\\b.mp3");//second mp3
                    while ((count = in.read(buffer)) != -1) {
                        os.write(buffer, 0, count);
                        os.flush();
                    }
                    in.close();
                    os.close();




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