Java 空指针异常增益控制
以下代码段应调节AAC播放器的音量:Java 空指针异常增益控制,java,audio,Java,Audio,以下代码段应调节AAC播放器的音量: private void decodeAndPlayAAC() { SourceDataLine line = null; byte[] b; try { stop = false; final ADTSDemultiplexer adts = new ADTSDemultiplexer(url.openStream()); final Decoder dec = new Decoder
private void decodeAndPlayAAC() {
SourceDataLine line = null;
byte[] b;
try {
stop = false;
final ADTSDemultiplexer adts = new ADTSDemultiplexer(url.openStream());
final Decoder dec = new Decoder(adts.getDecoderSpecificInfo());
final SampleBuffer buf = new SampleBuffer();
while (!stop) {
b = adts.readNextFrame();
//here the AACException for unexpected profile is thrown
dec.decodeFrame(b, buf);
FloatControl gainControl = null;
if (line == null) {
final AudioFormat audioFormat = new AudioFormat(buf.getSampleRate(), buf.getBitsPerSample(), buf.getChannels(), true, true);
line = AudioSystem.getSourceDataLine(audioFormat);
line.open();
gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
line.start();
addAudioDetails(audioFormat);
}
b = buf.getData();
line.write(b, 0, b.length);
int gainLevel = (int) ((int) gainControl.getMinimum() + ((gainControl.getMaximum() - gainControl.getMinimum()) / 100 * gainPercent));
gainControl.setValue(gainLevel);
}
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (AACException e) {
e.printStackTrace();
WebradioPlayer.getPlayer().getIcyReader().setInterrupted(true);
WebradioPlayer.setPlayer(null);
GUIHandler.getInstance().resetComponents();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (line != null) {
line.stop();
line.close();
stop = true;
GUIHandler.getInstance().resetComponents();
}
}
}
NullPointerException发生在gainControl上。当我在第一次“运行”中调试此代码时,gainControl在那里,但在第二次运行中它为null。
有人能给我解释一下吗?这真的很尴尬:D,我不应该在每次运行时都将gainControl设置为null….请发布堆栈跟踪。。。哪一行抛出了
NullPointerException
?问题已经解决,我没有看到每次运行while循环时都将gainControl设置为null。抱歉:)好的,NoProblemException
;-)捕获(无问题异常){:}