有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java空指针异常增益控制

下面的代码片段应该可以调节AAC播放器的音量:

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。 有人能给我解释一下吗


共 (0) 个答案