2015-04-16 189 views
16

我想獲得兩個音頻文件作爲輸入,然後將它們按字節合併並保存爲單個文件。
在這段代碼中,我試圖用Java來做,它工作的很好,但我不知道如何在android中做到這一點。如何將兩個音頻文件合併爲一個Android音頻文件?

如何在android中做到這一點?

import java.io.File; 
import java.io.InputStream; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.nio.file.Path; 

import javax.sound.sampled.AudioFileFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 

public class FileMixer { 
    public static void main(String[] args) 
    { 
     try 
     { 
    Path path1 = Paths.get("C:\\Srini\\Wav\\welcome.wav"); 
    Path path2 = Paths.get("C:\\Srini\\Wav\\goodbye.wav"); 
    String path3 ="C:\\Srini\\Wav\\srini12.wav"; 
    File Newfilepath=new File(path3); 

    byte[] byte1 = Files.readAllBytes(path1); 
    byte[] byte2 = Files.readAllBytes(path2); 
    byte[] out = new byte[byte1.length]; 
    for (int i=0; i<byte1.length; i++) 
    { 
     out[i] = (byte) ((byte1[i] + byte2[i]) >> 1); 
    } 

    InputStream byteArray = new ByteArrayInputStream(out); 
    AudioInputStream ais = AudioSystem.getAudioInputStream(byteArray); 
    AudioSystem.write(ais, AudioFileFormat.Type.WAVE,Newfilepath); 
    } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     } 
} 

回答

5
private void mergeSongs(File mergedFile,File...mp3Files){ 
     FileInputStream fisToFinal = null; 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(mergedFile); 
      fisToFinal = new FileInputStream(mergedFile); 
      for(File mp3File:mp3Files){ 
       if(!mp3File.exists()) 
        continue; 
       FileInputStream fisSong = new FileInputStream(mp3File); 
       SequenceInputStream sis = new SequenceInputStream(fisToFinal, fisSong); 
       byte[] buf = new byte[1024]; 
       try { 
        for (int readNum; (readNum = fisSong.read(buf)) != -1;) 
         fos.write(buf, 0, readNum); 
       } finally { 
        if(fisSong!=null){ 
         fisSong.close(); 
        } 
        if(sis!=null){ 
         sis.close(); 
        } 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }finally{ 
      try { 
       if(fos!=null){ 
        fos.flush(); 
        fos.close(); 
       } 
       if(fisToFinal!=null){ 
        fisToFinal.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
+1

它僅增加大小,,,但是當我打開它,只有第一音頻播放。 –

+0

相同的問題...第二個音頻不播放...... –

+0

你面臨什麼問題 –

1

對於組合兩個wav文件使用此代碼,

import java.io.File; 
import java.io.IOException; 
import java.io.SequenceInputStream; 
import javax.sound.sampled.AudioFileFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 

public class WavAppender { 
    public static void main(String[] args) { 
     String wavFile1 = "D:\\wavOne.wav"; 
     String wavFile2 = "D:\\wavTwo.wav"; 

     try { 
      AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1)); 
      AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2)); 

      AudioInputStream appendedFiles = 
          new AudioInputStream(
           new SequenceInputStream(clip1, clip2),  
           clip1.getFormat(), 
           clip1.getFrameLength() + clip2.getFrameLength()); 

      AudioSystem.write(appendedFiles, 
          AudioFileFormat.Type.WAVE, 
          new File("D:\\wavAppended.wav")); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
相關問題