Java之多个wav音频文件合并成一个
Java之多个wav音频文件合并成一个,实现语音播报:
1 package com.tp.test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.SequenceInputStream; 7 import java.util.ArrayList; 8 import java.util.Collections; 9 import java.util.Enumeration; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Scanner; 13 14 import javax.sound.sampled.AudioFileFormat; 15 import javax.sound.sampled.AudioInputStream; 16 import javax.sound.sampled.AudioSystem; 17 import javax.sound.sampled.UnsupportedAudioFileException; 18 19 import com.sun.xml.internal.ws.message.StringHeader; 20 import com.tp.util.StringHelpers; 21 22 23 public class WavComposeUtils { 24 25 public WavComposeUtils() { 26 // TODO Auto-generated constructor stub 27 } 28 public static void main(String[] args) { 29 // WavCompose("15130961639", "151309",5,"aaa"); 30 } 31 32 public static void WavCompose(String str, String uuid,int number,String planid) 33 { 34 String telNumChinese = toTelNumChinese(str); 35 telNumChinese = telNumChinese.substring(telNumChinese.length() - number, telNumChinese.length()); 36 List listUrl = new ArrayList(); 37 38 listUrl.add("C:\\Users\\xiaos\\Desktop\\1-0\\shou.wav"); 39 for (int i = 0; i < telNumChinese.length(); i++) 40 listUrl.add((new StringBuilder("C:\\Users\\xiaos\\Desktop\\1-0\\")).append(telNumChinese.charAt(i)).append(".wav").toString()); 41 listUrl.add((new StringBuilder("/data/voiceRecord/"+planid+"/1-0/")).append(telNumChinese.charAt(i)).append(".wav").toString()); 42 43 44 listUrl.add("C:\\Users\\xiaos\\Desktop\\1-0\\wei.wav"); 45 46 47 String mubiaoUrl = (new StringBuilder("C:\\Users\\xiaos\\Desktop\\zhenghe\\")).append(uuid).append(".wav").toString(); 48 File hebingUrl = new File(mubiaoUrl); 49 List list = new ArrayList(); 50 File file2 = null; 51 for (int i = 0; i < listUrl.size(); i++) 52 { 53 file2 = new File((String)listUrl.get(i)); 54 list.add(file2); 55 } 56 57 try 58 { 59 mergeFile((File)list.get(0), list, hebingUrl); 60 } 61 catch (IOException e) 62 { 63 e.printStackTrace(); 64 } 65 catch (UnsupportedAudioFileException e) 66 { 67 e.printStackTrace(); 68 } 69 } 70 71 /** 72 * wav语音文件合成,多文件合并 73 * 74 */ 75 public static void mergeFile(File indexFile, List<File> listFile, File descFile) 76 throws IOException, UnsupportedAudioFileException { 77 AudioFileFormat aff = AudioSystem.getAudioFileFormat(indexFile); 78 AudioInputStream ais1 = null; 79 List<InputStream> inp = new ArrayList<InputStream>(); 80 long sum = 0; 81 for (int i = 0; i < listFile.size(); i++) { 82 ais1 = AudioSystem.getAudioInputStream(listFile.get(i)); 83 inp.add(ais1); 84 sum += ais1.getFrameLength(); 85 } 86 Enumeration<InputStream> e = Collections.enumeration(inp); 87 SequenceInputStream sis = new SequenceInputStream(e); 88 ais1.getFrameLength(); 89 if (sum != 0) { 90 AudioSystem.write(new AudioInputStream(sis, aff.getFormat(), sum), aff.getType(), descFile); 91 } 92 if (ais1 != null) 93 ais1.close(); 94 if (sis != null) 95 sis.close(); 96 } 97 98 /** 99 * 手机号码转大写 100 * 101 */ 102 public static String toTelNumChinese(String telNum) { 103 // String[] s1 = { "零", "要", "二", "三", "四", "五", "六", "七", "八", "九" }; 104 String[] s1 = { "0", "要", "2", "3", "4", "5", "6", "7", "8", "9" }; 105 String result = ""; 106 int n = telNum.length(); 107 for (int i = 0; i < n; i++) { 108 int num = telNum.charAt(i) - '0'; 109 result += s1[num]; 110 } 111 return result; 112 } 113 }