2012-08-04 57 views
1

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

你只是想将文件连接成一个大文件,或者你想'混合'他们,所以一个文件播放2首歌曲? – 2012-08-04 08:07:03

+0

我想连接成一个大文件。可能吗? – user1573066 2012-08-04 08:09:21

+0

你的'str'不包含合法的文件名。 – 2012-08-04 08:11:28

回答

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文件读取它

+0

因此,我做了上述代码后,我可以将它们加入一个文件? – user1573066 2012-08-04 08:20:40

+0

我这么认为。然而,MP3将可能是无用的,无法播放 – 2012-08-04 08:22:53

+0

嗯..我需要它玩..有什么在Java中,我可以使用这个? – user1573066 2012-08-04 08:58:11

-1

什么是你想对于...实际上..如果你想读取2个文件到字节流不String str = "D:/Music/Assembled/Heart001.mp3" + "D:/Music/Assembled/Heart002.mp3"; 使str1=D:/Music/Assembled/Heart001.mp3str2=D:/Music/Assembled/Heart002.mp3和读str1,str2分别通过bufferedoutputsream

+0

好的,谢谢,我会尽力 – user1573066 2012-08-04 08:22:57

+0

它有相同的结果。我需要它结合两个MP3文件的数据,并能够发挥它合并后 – user1573066 2012-08-04 08:58:55

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

不起作用......它只是连接两个文件并忽略标题等 – wutzebaer 2014-03-21 20:53:18

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