简体   繁体   English

将两个mp3文件合并为一个

[英]Joining two mp3 files into one

I have this code to read bytes to another file. 我有这段代码可以读取字节到另一个文件。 But I'm not able to concatenate two mp3 files into one. 但我无法将两个mp3文件合并为一个。 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");

It`s suck. 糟透了。 Mp3 file starts with headers. mp3文件以标题开头。 For correct merging you have to skip first 32 bytes. 为了正确合并,您必须跳过前32个字节。 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();
        }

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 如您所见,您需要打开mp3文件的代码才能读取它

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"; 实际上,如果您想读取2个文件以字节流的形式,则不要尝试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 使str1=D:/Music/Assembled/Heart001.mp3str2=D:/Music/Assembled/Heart002.mp3并通过bufferedoutputsream分别读取str1,str2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM