views:

1174

answers:

2

I'm using JLayer to play an inputstream of mp3 data from the internet. How do i change the volume of the output?

I'm using this code to play it:

URL u = new URL(s);
URLConnection conn = u.openConnection();  
conn.setConnectTimeout(Searcher.timeoutms);  
conn.setReadTimeout(Searcher.timeoutms);

bitstream = new Bitstream(conn.getInputStream()/*new FileInputStream(quick_file)*/);
System.out.println(bitstream);
decoder = new Decoder();
decoder.setEqualizer(equalizer);
audio = FactoryRegistry.systemRegistry().createAudioDevice();
audio.open(decoder);
for(int i = quick_positions[0]; i > 0; i--){
 Header h = bitstream.readFrame();
 if (h == null){
     return;
 }
bitstream.closeFrame();
A: 

Try adjusting all bands of the equalizer for the volume you’re trying to get. Maybe you can also use an EQFunction for that?

public class ChangeVolumeFunction extends EQFunction {
    public double volume = 1.0;
    public void setVolume(double volume) { this.volume = volume; }
    public float getBand(int band) { return (float) (Math.log(volume) / Math.log(2) * 6.0); }
}
Bombe
you forgot the "class" after public. nitpicky :) I'm trying it right now.
Penchant
doesnt seem to work :(
Penchant
+1  A: 

You may consider this an unacceptable hack, but it worked in an MP3 player I wrote. It requires a small code addition to one of the JLayer classes.

Add the following method to javazoom.jl.player.JavaSoundAudioDevice.

public void setLineGain(float gain)
{
    if (source != null)
    {
        FloatControl volControl = (FloatControl) source.getControl(FloatControl.Type.MASTER_GAIN);
        float newGain = Math.min(Math.max(gain, volControl.getMinimum()), volControl.getMaximum());

        volControl.setValue(newGain);
    }
}

This new method then allows you to change the volume with code like this.

if (audio instanceof JavaSoundAudioDevice)
{
    JavaSoundAudioDevice jsAudio = (JavaSoundAudioDevice) audio;
    jsAudio.setLineGain(yourGainGoesHere);
}
ChrisH