我正在使用Javascript / HTML5 / JSF前端和Glassfish(Java)后端编写基于Web的录音机。
我需要使用ULAW编码保存录制的.WAV文件。但是据我所知,在HTML5 / Javascript(带getUserMedia()
)中录制音频的唯一方法是使用PCM编码。我想用一种简单的方法来捕获ULAW中的客户端录音,但一直无法找到任何方法。
所以我现在要做的是:
Record in PCM wav (client side)
Upload to server using JSP
Pass the received FileItem into a JAVA converter method that returns a byte array in ULAW
我发现以下有人试图在Android中执行此操作:
Android PCM to Ulaw encoding wav file
然而,本文引用的类要么不起作用,要么我没有正确使用它。
我当前的convertPCMtoULAW(FileItem file)
Java方法:
//read the FileItem's InputStream into a byte[]
InputStream uploadedStream = file.getInputStream();
byte[] pcmBytes = new byte[(int) file.getSize()];
uploadedStream.read(pcmBytes);
//extract the number of PCM Samples from the header
int offset =40;
int length =4;
int pcmSamples = 0;
for (int i = 0; i < length; i++)
{
pcmSamples += ((int) pcmBytes[offset+i] & 0xffL) << (8 * i);
}
//create the UlawEncoderInputStream (class in link above)
is = new UlawEncoderInputStream(
file.getInputStream(),
UlawEncoderInputStream.maxAbsPcm(pcmBytes, 44, pcmSamples/2)
);
//read from the created InputStream into another byte[]
byteLength = is.read(ulawBytes);
//I then add the ULAW header to the beginning of the byte[]
//and pass the entire byte[] through a pre-existing Wav Verifier method
//which serializes the byte[] later on
(所有代码编译,上面的内容被简化为包含必要的部分)
我一直只在变量byteLength中读回512字节。
我知道我正在将正确的PCM WAV上传到Glassfish,因为我可以直接从Javascript端下载并收听我的录音。
在我尝试编码后,在服务器端打开文件时出现错误。
我的主要问题是:有没有人能够成功使用链接页面中的类从PCM编码到ULAW?
答案 0 :(得分:0)
我认为你所面临的问题与转码没有任何关系,而是与Java InputStream.read
的合同有关。来自documentation:
从输入流中读取一些字节数并将它们存储到缓冲区数组b中。实际读取的字节数以整数形式返回。此方法将阻塞,直到输入数据可用,检测到文件结尾或引发异常。
换句话说,此函数返回的数字是该方法的特定调用中实际读取的字节数。合同并不保证它会在流关闭之前读取所有字节,只有在调用它时可用的字节数。
您必须在循环中调用其重载read(byte[] b, int off, int len)
,直到关闭流,或将流包装到DataInputStream
并使用readFully
。