gpt4 book ai didi

java - JLayer 总是抛出 ArrayIndexOutOfBounds 异常

转载 作者:行者123 更新时间:2023-11-30 09:15:29 24 4
gpt4 key购买 nike

我正在使用 JLayer 解码 MP3,并将其存储为 PCM 数据。但是,无论我使用什么 MP3 文件,我总是会收到索引为 443 的 ArrayIndexOutOfBoudnsException。我听说这只发生在某些 MP3 上,但它发生在我播放的每个 MP3 的每一帧上这是我的代码:

private short[] getPCM(Header frameHeader, Bitstream bs) {
short[] pcm = null;
try {
Decoder d = new Decoder();
SampleBuffer buffer = (SampleBuffer) d.decodeFrame(frameHeader, bs);
pcm = buffer.getBuffer();
} catch (ArrayIndexOutOfBoundsException | DecoderException e) {
System.err.println("JLayer, stap it");
}
return pcm;
}

然后使用以下代码调用该方法:

while ((frameHeader = bs.readFrame()) != null) {
short[] pcm = getPCM(frameHeader, bs);

for(short i : pcm){
try {
os.write(i);
} catch (IOException e) {
e.printStackTrace();
}
}
}

堆栈跟踪:

java.lang.ArrayIndexOutOfBoundsException: 433
at javazoom.jl.decoder.Bitstream.get_bits(Unknown Source)
at javazoom.jl.decoder.LayerIIIDecoder.decode(Unknown Source)
at javazoom.jl.decoder.LayerIIIDecoder.decodeFrame(Unknown Source)
at javazoom.jl.decoder.Decoder.decodeFrame(Unknown Source)
at com.dentonposs.Canvas.getPCM(Canvas.java:70)
at com.dentonposs.Canvas.paintComponent(Canvas.java:48)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

您需要在每次调用 readFrame() 之间调用 Bitstream.closeFrame() - 所以

while ((frameHeader = bs.readFrame()) != null) {
short[] pcm = getPCM(frameHeader, bs);

bs.closeFrame();

.. more ..

关于java - JLayer 总是抛出 ArrayIndexOutOfBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19893854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com