将两个mp3文件合二为一

时间:2012-08-04 08:04:58

标签: java mp3

我有这个代码将字节读取到另一个文件。 但是我无法将两个mp3文件连接成一个。 我错过了什么吗?

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");

4 个答案:

答案 0 :(得分:1)

太糟糕了。 Mp3文件以标头开头。要正确合并,您必须跳过前32个字节。试试这个。

 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();
        }

答案 1 :(得分:0)

您需要分两步完成此操作

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

正如您所看到的,您需要使用代码打开mp3文件才能阅读

答案 2 :(得分:-1)

你在尝试什么...实际上..如果你想要读取2个文件到字节流不要String str = "D:/Music/Assembled/Heart001.mp3" + "D:/Music/Assembled/Heart002.mp3"; make str1=D:/Music/Assembled/Heart001.mp3str2=D:/Music/Assembled/Heart002.mp3并通过bufferedoutputsream单独读取str1,str2

答案 3 :(得分:-1)

此代码可以正常工作,并在几秒钟内合并类似类型的音频...

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();
            }
相关问题