Volume control with JLayer

Me and a friend are programming an MP3 player as a school project. We are almost done and are now stuck at the point where we are trying to program a function to change the volume of the player. We use:

  • Audiodevice
  • Advancedplayer

I know that someone else asked the same question allready, but I did not quite understand this solution, and I did not want to answer such an old question, so I thought that I would just try again.

Cheers Timothy

0
source share
2 answers

- jlayer mp3spi, jlayer JavaSound. , JavaSound.

-, :

  • jl1.0.1.jar
  • mp3spi1.9.5.jar
  • tritonus_share.jar

... mp3spi (. ).

-, AudioInputStream.

AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = audioStream.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
        baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
AudioInputStream audioStream2 = AudioSystem.getAudioInputStream(decodedFormat, audioStream);

:

Clip clip = AudioSystem.getClip();
clip.open(audioStream2);

JavaSound API:

FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-30.0f);

. , - JavaSound, .

+2

JLGUI - JLayer, . tar.gz. http://www.javazoom.net/jlgui/sources.html

    if (src == ui.getAcVolume())
    {
        Object[] args = { String.valueOf(ui.getAcVolume().getValue()) };
        String volumeText = MessageFormat.format(ui.getResource("slider.volume.text"), args);
        ui.getAcTitleLabel().setAcText(volumeText);
        try
        {
            int gainValue = ui.getAcVolume().getValue();
            int maxGain = ui.getAcVolume().getMaximum();
            if (gainValue == 0) theSoundPlayer.setGain(0);
            else theSoundPlayer.setGain(((double) gainValue / (double) maxGain));
            config.setVolume(gainValue);
        }
        catch (BasicPlayerException ex)
        {
            log.debug("Cannot set gain", ex);
        }
    }
0

All Articles