2013-11-25 104 views
3

我有產生許多.mp3文件,我需要將其合併到關於電子MP3文件的,我試試這個代碼:合併.mp3文件合併成一個文件.MP3的Android

FileInputStream fist = null; 
    try { 
     fist = new FileInputStream("/mnt/sdcard/Flash Library/Resources/sound1.mp3"); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    FileInputStream fist2 = null; 
    try { 
     fist2 = new FileInputStream("/mnt/sdcard/Flash Library/Resources/sound2.mp3"); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     File dir = new File ("/mnt/sdcard/Flash Library/dir1"); 
     dir.mkdirs(); 

     SequenceInputStream sistream = new SequenceInputStream(fist, fist2); 

     FileOutputStream fostream = null; 
     try { 
      fostream = new FileOutputStream("/mnt/sdcard/Flash Library/dir1/output.mp3"); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     int temp; 
     try { 
      while((temp = sistream.read()) != -1) 
      { 
       Toast.makeText(ctx.getActivity(), sistream.toString(), Toast.LENGTH_SHORT).show(); 
       // System.out.print((char) temp); // to print at DOS prompt 
       fostream.write(temp); // to write to file 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      fostream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      sistream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      fist.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      fist2.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    return null; 

首先,它會生成文件(output.mp3)只有第一個mp3數據,第二個不會添加到輸出,其次我需要合併很多mp3文件不只是兩個? 我搜索合並MP3文件和上面的例子中推薦的所有。 有沒有解決我的問題的方法?

+0

你有什麼問題@Sameer H.伊布拉? – GrIsHu

+0

@GrIsHu:我需要一個將多個.mp3文件合併到一個文件中的庫? –

+0

如果您發現解決方案,請告訴我,我也有同樣的問題 –

回答

0

它似乎通過將一個人的內容添加到另一個人的最終作品來合併.mp3文件(因爲您的代碼也顯示出來了)。然後,它應該是很容易寫一個:(未經測試)

public void mergeMp3Files(File[] inputs, File output) throws IOException { 
    FileOutputStream outputStream = null; 
    try { 
     outputStream = new FileOutputStream(output); 
     for (File input : inputs) { 
      FileInputStream inputStream = null; 
      try { 
       IOUtils.copy(inputStream = new FileInputStream(input), 
          outputStream); 
      } finally { 
       if (inputStream != null) try { 
        inputStream.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } finally { 
     if (outputStream != null) try { 
      outputStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

如果你沒有一個IOUtils.copy呢,這裏有一個適合你:

public class IOUtils { 
    private static final int BUF_SIZE = 0x1000; // 4K 

    public static long copy(InputStream from, OutputStream to) 
      throws IOException { 
     byte[] buf = new byte[BUF_SIZE]; 
     long total = 0; 
     while (true) { 
      int r = from.read(buf); 
      if (r == -1) { 
       break; 
      } 
      to.write(buf, 0, r); 
      total += r; 
     } 
     return total; 
    } 
} 
相關問題