将多个wave文件连接到java中的一个文件中

时间:2017-04-21 13:29:59

标签: java ios audioinputstream

public static Boolean concatenateFiles(String destinationFileName) throws Exception 

{

Boolean result = false;

        AudioInputStream audioInputStream = null;
        ArrayList<AudioInputStream> audioInputStreamList_2 = null;
        AudioFormat audioFormat = null;
        Long frameLength = null;
        String[] sourceFileList_Final ;
        destinationFileName="C://MyFolder//result.wav";
        sourceFileList_Final= dir.list(); 

 try{   

     // loop through our files first and load them up

     for (String sourceFileList_Final1 : sourceFileList_Final) {
         audioInputStream = AudioSystem.getAudioInputStream(new File(sourceFileList_Final1));

         // get the format of first file
         if (audioFormat == null) {
             audioFormat = audioInputStream.getFormat();
         }
         // add it to our stream list
         if (audioInputStreamList_2 == null) {
             audioInputStreamList_2 = new ArrayList<AudioInputStream>();
         }
        //try{ audioInputStreamList_2.add(audioInputStream);} catch( NullPointerException | IllegalArgumentException ex ){}
        audioInputStreamList_2.add(audioInputStream);
         // keep calculating frame length
         if(frameLength == null) {
             frameLength = audioInputStream.getFrameLength();
         }
         else {
             frameLength += audioInputStream.getFrameLength();
         }
     }

现在编写我们的连接文件

    try {AudioSystem.write(new AudioInputStream(new SequenceInputStream(Collections.enumeration(audioInputStreamList_2)), 
           audioFormat, frameLength), 
              AudioFileFormat.Type.WAVE, new File(dir,destinationFileName));} 
      catch(NullPointerException  | IOException e){throw e;}
// if all is good, return true

            result = true;
        }
                catch(NullPointerException | UnsupportedAudioFileException | IOException e){throw e;}

        finally {
            if (audioInputStream != null) {
                audioInputStream.close();
            }
            if (audioInputStreamList_2 != null) {
                audioInputStreamList_2 = null;
            }
        }
            catch(NullPointerException ne){}

 return result ;

}

0 个答案:

没有答案