java wav 读取,用Java读取WAV文件的问题

在Java中使用API读取WAV文件并将其内容存入ArrayList时,遇到数组大小不匹配的问题。原始代码在最后一轮复制时超过了实际读取的帧数。已修复该错误,但程序运行时偶尔出现`InterruptedException`。问题可能源于线程等待或资源释放操作。解决方案是确保在处理缓冲区时考虑实际读取的帧数。
摘要由CSDN通过智能技术生成

I use that API to read wav file at Java: Reading and Writing Wav Files in Java

I read a wav file with Java and want to put all the values into an arraylist. I wrote:

// Open the wav file specified as the first argument

WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence);

List allBuffer = new ArrayList();

....

do {

// Read frames into buffer

framesRead = wavFile.readFrames(buffer, 50);

for (double aBuffer : buffer) {

allBuffer.add(aBuffer);

}

} while (framesRead != 0);

When I check the size of allBuffer at debugger it says:

74700

However at the code it whe I use:

wavFile.display();

the output of it:

....

... Frames: 74613

....

My allbuffer is bigger than frames as displayed by that API. How it comes?

PS: My previous questions about this:

PS2:

I fixed the bugs however when I run the program I get that error sometimes: What may be the problem?

java.lang.InterruptedException

at java.lang.Object.wait(Native Method)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)

at sun.java2d.Disposer.run(Disposer.java:127)

at java.lang.Thread.run(Thread.java:662)

Exception while removing reference: java.lang.InterruptedException

解决方案

you need to account for framesRead when copying from the buffer into your list. on the last call, you are copying more doubles than you actually read from the file. (i.e. exactly like the example code in your first link).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值