用來剪輯特定長度的音頻,並將它們混剪在一起,大體思路是這樣的:
1. 使用 FileInputStream 輸入兩個音頻
2. 使用 FileInputStream的skip(long n) 方法跳過特定字節長度的音頻文件,比如說:輸入 skip(1024*1024*3),這樣就能丟棄掉音頻文件前面的 3MB 的內容。
3. 截取中間特定長度的音頻文件:每次輸入 8KB 的內容,使用 count 記錄輸入次數,達到設置的次數就終止音頻輸入。比如說要截取 2MB 的音頻,每次往輸入流中輸入 8KB 的內容,就要輸入 1024*2/8 次。
4. 往同一個輸出流 FileOutputStream 中輸出音頻,並生成文件,實現音頻混合。
public class MusicCompound { public static void main(String args[]) { FileOutputStream fileOutputStream = null; FileInputStream fileInputStream = null; String fileNames[] = {"E:/星月神話.mp3","E:/我只在乎你.mp3"}; //設置byte數組,每次往輸出流中傳入8K的內容 byte by[] = new byte[1024*8]; try { fileOutputStream = new FileOutputStream("E:/合並.mp3"); for(int i=0;i<2;i++) { int count = 0; fileInputStream = new FileInputStream(fileNames[i]); //跳過前面3M的歌曲內容 fileInputStream.skip(1024*1024*3); while(fileInputStream.read(by) != -1) { fileOutputStream.write(by); count++; System.out.println(count); //要截取中間2MB的內容,每次輸入8k的內容,所以輸入的次數是1024*2/8 if(count == (1024*2/8)) { break; } } } } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } finally { try { //輸出完成后關閉輸入輸出流 fileInputStream.close(); fileOutputStream.close(); } catch(IOException e) { e.printStackTrace(); } } } }