Java实现多个wav文件合成一个的方法示例
作者:大爱一生 时间:2021-10-08 08:14:25
本文实例讲述了Java实现多个wav文件合成一个的方法。分享给大家供大家参考,具体如下:
前面一篇介绍了java切割wav音频文件的方法,这里再给出合并多个wav音频文件的方法。
package com.cmos.nomsapp.utils.wavmeger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
/**
* 获取wav头文件然后合并成单个wav
* @author zcf
* @date 2017-10-17
*/
public class WavMergeUtil {
/**
* meger多个wav
* @param inputs 多个wav
* @param output 要生成的wav
* @throws IOException
*/
public static void mergeWav(File[] inputs, String output) throws IOException {
if (inputs.length < 1) {
return;
}
try(FileInputStream fis = new FileInputStream(inputs[0]);
FileOutputStream fos = new FileOutputStream(new File(output))){
byte[] buffer = new byte[1024 * 4];
int total = 0;
int count;
while ((count = fis.read(buffer)) > -1) {
fos.write(buffer, 0, count);
total += count;
}
fis.close();
for (int i = 1; i < inputs.length; i++) {
File file = inputs[i];
try(FileInputStream fisH = new FileInputStream(file)){
Header header = resolveHeader(fisH);
FileInputStream dataInputStream = header.dataInputStream;
while ((count = dataInputStream.read(buffer)) > -1) {
fos.write(buffer, 0, count);
total += count;
}
}
}
fos.flush();
fos.close();
FileInputStream fisHo = new FileInputStream(new File(output));
Header outputHeader = resolveHeader(fisHo);
outputHeader.dataInputStream.close();
try(RandomAccessFile res = new RandomAccessFile(output, "rw")){
res.seek(4);
byte[] fileLen = intToByteArray(total + outputHeader.dataOffset - 8);
res.write(fileLen, 0, 4);
res.seek(outputHeader.dataSizeOffset);
byte[] dataLen = intToByteArray(total);
res.write(dataLen, 0, 4);
}
}
}
/**
* 解析头部,并获得文件指针指向数据开始位置的InputStreram,记得使用后需要关闭
*/
private static Header resolveHeader(FileInputStream fis) throws IOException {
byte[] byte4 = new byte[4];
byte[] buffer = new byte[2048];
int readCount = 0;
Header header = new Header();
fis.read(byte4);// RIFF
fis.read(byte4);
readCount += 8;
header.fileSizeOffset = 4;
header.fileSize = byteArrayToInt(byte4);
fis.read(byte4);// WAVE
fis.read(byte4);// fmt
fis.read(byte4);
readCount += 12;
int fmtLen = byteArrayToInt(byte4);
fis.read(buffer, 0, fmtLen);
readCount += fmtLen;
fis.read(byte4);// data or fact
readCount += 4;
if (isFmt(byte4, 0)) {// 包含fmt段
fis.read(byte4);
int factLen = byteArrayToInt(byte4);
fis.read(buffer, 0, factLen);
fis.read(byte4);// data
readCount += 8 + factLen;
}
fis.read(byte4);// data size
int dataLen = byteArrayToInt(byte4);
header.dataSize = dataLen;
header.dataSizeOffset = readCount;
readCount += 4;
header.dataOffset = readCount;
header.dataInputStream = fis;
return header;
}
private static boolean isRiff(byte[] bytes, int start) {
if (bytes[start + 0] == 'R' && bytes[start + 1] == 'I' && bytes[start + 2] == 'F' && bytes[start + 3] == 'F') {
return true;
} else {
return false;
}
}
private static boolean isFmt(byte[] bytes, int start) {
if (bytes[start + 0] == 'f' && bytes[start + 1] == 'm' && bytes[start + 2] == 't' && bytes[start + 3] == ' ') {
return true;
} else {
return false;
}
}
private static boolean isData(byte[] bytes, int start) {
if (bytes[start + 0] == 'd' && bytes[start + 1] == 'a' && bytes[start + 2] == 't' && bytes[start + 3] == 'a') {
return true;
} else {
return false;
}
}
/**
* 将int转化为byte[]
*/
private static byte[] intToByteArray(int data) {
return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(data).array();
}
/**
* 将short转化为byte[]
*/
private static byte[] shortToByteArray(short data) {
return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(data).array();
}
/**
* 将byte[]转化为short
*/
private static short byteArrayToShort(byte[] b) {
return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
}
/**
* 将byte[]转化为int
*/
private static int byteArrayToInt(byte[] b) {
return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
/**
* 头部部分信息
*/
static class Header {
public int fileSize;
public int fileSizeOffset;
public int dataSize;
public int dataSizeOffset;
public int dataOffset;
public FileInputStream dataInputStream;
}
}
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/weixin_39372979/article/details/80493719
标签:Java,wav文件,合成
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现将数字转换成人民币大写
2023-08-11 05:07:29
java字符串常用操作方法(查找、截取、分割)
2023-11-29 03:21:13
Springmvc Controller接口代码示例
2023-11-28 10:13:25
解析Flink内核原理与实现核心抽象
2023-11-25 10:30:04
![](https://img.aspxhome.com/file/2023/8/59868_0s.png)
值得收藏的2017年Java开发岗位面试题
2023-11-29 15:22:01
详解Java变量与常量
2023-11-09 21:00:14
![](https://img.aspxhome.com/file/2023/5/59005_0s.png)
Java详细讲解堆排序与时间复杂度的概念
2023-10-20 02:00:11
![](https://img.aspxhome.com/file/2023/7/58597_0s.png)
Java接口默认方法带来的问题分析【二义性问题】
2023-11-27 20:32:55
![](https://img.aspxhome.com/file/2023/7/60117_0s.png)
基于Java语言MD5加密Base64转换方法
2023-11-24 00:22:41
java关键字final使用方法详解
2023-11-28 22:38:04
Java对象类型的判断详解
2023-07-26 09:55:07
Java8之lambda最佳实践_动力节点Java学院整理
2023-11-28 00:07:28
springboot对接支付宝支付接口(详细开发步骤总结)
2023-11-10 23:07:35
![](https://img.aspxhome.com/file/2023/6/58746_0s.png)
在Java中避免NullPointerException的解决方案
2023-10-17 04:47:00
vue+springboot前后端分离工程跨域问题解决方案解析
2023-08-06 06:51:10
Kotlin中的惰性操作容器Sequence序列使用原理详解
2023-10-01 14:21:55
![](https://img.aspxhome.com/file/2023/7/84717_0s.jpg)
简单解析java方法在调用在内存中的执行过程
2022-04-12 15:46:26
![](https://img.aspxhome.com/file/2023/6/59756_0s.png)
java异常与错误处理基本知识
2023-11-25 10:44:59
java控制台实现学生信息管理系统
2023-11-29 02:05:20
解决mybatis批量更新出现SQL报错问题
2023-11-29 04:12:47
![](https://img.aspxhome.com/file/2023/5/60155_0s.png)