以下代码段应调节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。
有人可以向我解释吗?

最佳答案

这真是令人尴尬:D,我不应该每次运行时都将增益控制设置为null...。

10-06 02:20