讯飞录音,把几个pcm合成为wav

由于讯飞录音是不能把一段很长的话录音出来,只能是一段一段的录,所以要把录出来的pcm数据合成wav文件,录音的采样率是8k, 比特率是16bit,双声道,合并的代码如下:

<span style="font-size:14px;">public static boolean mergePCMFilesToWAVFile(ArrayList<String> fileList,
			String DESTINATION_PATH) {
		File[] file = new File[fileList.size()];
		byte buffer[] = null;

		int TOTAL_SIZE = 0;
		int FILE_NUMBER = fileList.size();

		for (int i = 0; i < FILE_NUMBER; i++) {
			file[i] = new File(fileList.get(i));
			TOTAL_SIZE += file[i].length();
		}

		// 填入参数,比特率等等。这里用的是16位单声道 8000 hz
		WaveHeader header = new WaveHeader();
		// 长度字段 = 内容的大小(TOTAL_SIZE) +
		// 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节)
		header.fileLength = TOTAL_SIZE + (44 - 8);
		header.FmtHdrLeth = 16;
		header.BitsPerSample = 16;
		header.Channels = 2;
		header.FormatTag = 0x0001;
		header.SamplesPerSec = 8000;
		header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8);
		header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
		header.DataHdrLeth = TOTAL_SIZE;

		byte[] h = null;
		try {
			h = header.getHeader();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		if (h.length != 44) // WAV标准,头部应该是44字节,如果不是44个字节则不进行转换文件
			return false;

		//先删除目标文件
		File destfile = new File(DESTINATION_PATH);
		if(destfile.exists())
			destfile.delete();
		
		//合成所有的pcm文件的数据,写到目标文件
		try {
			buffer = new byte[1024 * 4]; // Length of All Files, Total Size
			InputStream inStream = null;
			OutputStream ouStream = null;

			ouStream = new BufferedOutputStream(new FileOutputStream(
					DESTINATION_PATH));
			ouStream.write(h, 0, h.length);
			for (int j = 0; j < FILE_NUMBER; j++) {
				inStream = new BufferedInputStream(new FileInputStream(file[j]));
				int size = inStream.read(buffer);
				while (size != -1) {
					ouStream.write(buffer);
					size = inStream.read(buffer);
				}
				inStream.close();
			}
			ouStream.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found " + e);
		} catch (IOException ioe) {
			System.out.println("Exception while reading the file " + ioe);
		}
		deleteFiles(fileList);
		System.out.println("Merge was executed successfully.!");
		return true;

	}</span>


你可能感兴趣的:(android,PCM)