2012-07-04 43 views
3

我正在android中工作。我想合併兩個wave文件並想創建第三個文件。爲此,我創建兩個輸入流,然後嘗試將這些輸入流寫入一個輸出流文件。Android無法合併兩個wave文件

這是我的代碼: -

File file = new File("/sdcard/z.wav"); 
     File file1 = new File("/sdcard/zz.wav"); 
     InputStream is = new FileInputStream(file); 
     InputStream is1 = new FileInputStream(file1); 

     // Get the size of the file 
     long length = file.length(); 
     long length1 = file1.length(); 


     // Create the byte array to hold the data 
     byte[] bytes = new byte[(int)length]; 
     byte[] bytes1 = new byte[(int)length1]; 

     // Read in the bytes 
     int offset = 0; 
     int numRead = 0; 
     while (offset < (int)length &&(numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
      offset += numRead; 
     } 

     int offset1 = 0; 
     int numRead1 = 0; 
     while (offset1 < (int)length1 &&(numRead1=is1.read(bytes1, offset1, bytes1.length-offset1)) >= 0) { 
      offset1 += numRead1; 
     } 





     FileOutputStream fos=new FileOutputStream("sdcard/guruu.wav"); 
     Log.v("Trying Activity","before first write"); 
     fos.write(bytes); 

     fos.write(bytes1,0,bytes1.length); 
     fos.write(bytes); 
     fos.close(); 

     is.close(); 
     is1.close(); 

當我玩輸出文件guruu.wav那麼文件1這只是播放文件,它是不是在玩file2的內容。請告訴我我做了什麼錯誤。有沒有其他方法可以做到這一點?

我很新,所以請不要投票。

預先感謝您。

回答

0

您沒有合併數據,只是簡單地將它連接起來。

因此你結束了含有一個文件:文件爲文件2文件

  • WAV文件頭1
  • 音頻數據1
  • WAV文件標頭文件2
  • 音頻數據

可以理解的是,當有人讀這個文件回來播放它時,他們將其視爲

  • WAV文件頭
  • 音頻數據
  • 在他們不連得考慮文件的末尾一些垃圾數據。

要真正合並這些文件,您需要正確考慮internal structure of the WAV files