0

me and a friend are programming a MP3 Player as a schoolproject. We are nearly finished and now stuck at the point where we try to programm a function to change the volume of the player. We are using:

  • AudioDevice
  • AdvancedPlayer

I know someone else asked the same question allready but I did not quite get the solution and I didn't want to respond to such an old question so I thought I'm just gonna ask again.

Cheers Timothy

Timothy
  • 608
  • 3
  • 10
  • 24

2 Answers2

2

The easiest way to do this is to use jlayer via mp3spi which basically means you use jlayer via JavaSound. You can then set gain on the line as you would in JavaSound.

Firstly, you will need to add the following to your classpath:

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

...all of which are in the distribution for mp3spi (linked above).

Secondly, you will need to decode the AudioInputStream prior to playback.

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);

Then you play the decoded stream:

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

and JavaSound API controls are available:

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

NOTE: Don't forget to close your resources, I've just shown the key points for this issue - familiarity with JavaSound is expected, read here.

pstanton
  • 35,033
  • 24
  • 126
  • 168
0

JLGUI is a good example of a UI-based JLayer app adjusting volume. You can get the source code in the tar.gz file. 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);
        }
    }
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • Hey thanks for the quick answer ! I'll check it out and ask again if I don't get it :-) – Timothy Jun 28 '10 at 16:37
  • Ohh emm could you just quickly tell me in which file the code you posted is found ? Cheers – Timothy Jun 28 '10 at 16:39
  • PlayerUI.java :) In \src\javazoom\jlgui\player\amp – Chris Dennett Jun 28 '10 at 17:02
  • Okay I found it but I don't get it :-/ . theSoundPlayer is a BasicController but I can't find that Class anywhere ... – Timothy Jun 28 '10 at 17:07
  • The volume adjsutment code is apparently in javazoom.jlgui.basicplayer.BasicPlayer but I can't find the file! There's only the player package. Annoying :) – Chris Dennett Jun 28 '10 at 17:07
  • http://www.koders.com/java/fid518DAD29BA2B356FB9FB827BB313929A8CBA2865.aspx?s=BasicPlayer.java – Chris Dennett Jun 28 '10 at 17:15
  • Cheers for the link ! And the search goes on hehe. Can't imgagine why the Jlayer devolepers didn't include the function directly into the jlayer libary – Timothy Jun 28 '10 at 17:18
  • It looks like you have to adjust the volume on the implementation level (where it uses the javax.sound classes) -- the abstraction literally doesn't have the adjustment in it :) – Chris Dennett Jun 28 '10 at 18:23
  • please check my answer , it doesn't work right now but I don't know why yet. I think I kind of have to like it with my AudioDevice but I've got no idea how ... – Timothy Jun 28 '10 at 18:37
  • this is a bit of a red herring answer. – pstanton Jul 10 '13 at 12:14