Adjusting Volume using JLayer
Asked Answered
L

2

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

Laruelarum answered 28/6, 2010 at 16:19 Comment(0)
L
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.

Leanora answered 10/7, 2013 at 12:36 Comment(0)
T
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);
        }
    }
Tychon answered 28/6, 2010 at 16:30 Comment(10)
Hey thanks for the quick answer ! I'll check it out and ask again if I don't get it :-)Laruelarum
Ohh emm could you just quickly tell me in which file the code you posted is found ? CheersLaruelarum
PlayerUI.java :) In \src\javazoom\jlgui\player\ampTychon
Okay I found it but I don't get it :-/ . theSoundPlayer is a BasicController but I can't find that Class anywhere ...Laruelarum
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 :)Tychon
koders.com/java/…Tychon
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 libaryLaruelarum
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 :)Tychon
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 ...Laruelarum
this is a bit of a red herring answer.Leanora

© 2022 - 2024 — McMap. All rights reserved.