package org.lib.speech.test;
import java.io.*;
import java.util.*;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
//生成的新.wav路径 File fileOut = new File("E:/java/apache-tomcat-7.0.77/webapps/JavaWeb/music/1.wav"); List<String> listArr = new ArrayList<String>(); listArr.add("E:/java/apache-tomcat-7.0.77/webapps/data/jyutping-wong-44100-v7/aak6.wav"); listArr.add("E:/java/apache-tomcat-7.0.77/webapps/data/jyutping-wong-44100-v7/1.wav"); listArr.add("E:/java/apache-tomcat-7.0.77/webapps/data/jyutping-wong-44100-v7/ang1.wav"); //如果这个语音大于 2 个 if (listArr.size() >= 2){ AudioInputStream audio1 = AudioSystem.getAudioInputStream(new File(dir+listArr.get(0))); AudioInputStream audio2 = AudioSystem.getAudioInputStream(new File(dir+listArr.get(1))); AudioInputStream audioBuild = new AudioInputStream( new SequenceInputStream(audio1, audio2), audio1.getFormat(), audio1.getFrameLength() + audio2.getFrameLength() ); AudioInputStream audio3; //大于两个时继续合并 for(int i = 2; i<listArr.size();i++){ audio3 = AudioSystem.getAudioInputStream(new File(dir+listArr.get(i))); audioBuild = new AudioInputStream( new SequenceInputStream(audioBuild, audio3), audioBuild.getFormat(), audioBuild.getFrameLength() + audio3.getFrameLength() ); } //生成语音 AudioSystem.write(audioBuild, AudioFileFormat.Type.WAVE, fileOut ); } else { //TODO::否则只有一个,直接返回语音路径 }
注意 , 生成的语音要 比特率 一致,否则声音会很难听,如果要改为一直,则可以用百度音乐右上角工具来改变