java实现多个mav文件拼接合并成一个mav文件,绝对有效
解决方法:
import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioTest {
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
//生成的新.wav路径
File fileOut = new File("F:/jcwork/new.wav");
List<String> listArr = new ArrayList<String>();
listArr.add("F:/jcwork/one.wav");
listArr.add("F:/jcwork/two.wav");
listArr.add("F:/jcwork/three.wav");
//如果这个语音大于 2 个
if (listArr.size() >= 2){
AudioInputStream audio1 = AudioSystem.getAudioInputStream(new File(listArr.get(0)));
AudioInputStream audio2 = AudioSystem.getAudioInputStream(new File(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(listArr.get(i)));
audioBuild = new AudioInputStream(
new SequenceInputStream(audioBuild, audio3),
audioBuild.getFormat(), audioBuild.getFrameLength() +
audio3.getFrameLength()
);
}
//生成语音
AudioSystem.write(audioBuild, AudioFileFormat.Type.WAVE, fileOut );
}
}
}