1

我有这个代码来读取字节到另一个文件。但我无法将两个 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

4 回答 4

2

真烂 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();
        }
于 2014-06-13T11:10:56.267 回答
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 文件来阅读它

于 2012-08-04T08:14:34.843 回答
-1

您在尝试什么...实际上..如果您想读取 2 个文件以进行字节流传输,则不要String str = "D:/Music/Assembled/Heart001.mp3" + "D:/Music/Assembled/Heart002.mp3"; 通过bufferedoutputsream 分别读取str1=D:/Music/Assembled/Heart001.mp3str2=D:/Music/Assembled/Heart002.mp3读取 str1、str2

于 2012-08-04T08:14:35.877 回答
-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();
            }
于 2013-03-25T06:20:41.663 回答