使用 JLayer 时更改 Java 中的音量

发布于 2024-07-14 15:15:00 字数 662 浏览 6 评论 0原文

我正在使用 JLayer 播放来自互联网的 mp3 数据输入流。 如何更改输出音量?

我正在使用这段代码来播放它:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法少女 2024-07-21 15:15:01

您可能认为这是不可接受的 hack,但它在我编写的 MP3 播放器中有效。 它需要向 JLayer 类之一添加一小段代码。

将以下方法添加到 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);
    }
}

然后,这个新方法允许您使用这样的代码更改音量。

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

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);
}
尐籹人 2024-07-21 15:15:01

调整 PC 扬声器音量的另一种(非 JLayer)方法是使用 Sun/Oracle 提供的 javax.sound.sampled 包中的类。

在我的机器上,SPEAKER 是唯一支持的输出目标。

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Port;

public class SoundVolumeDemo 
{

    public static void main(String[] args) 
{        
    Info source = Port.Info.SPEAKER;
    //        source = Port.Info.LINE_OUT;
    //        source = Port.Info.HEADPHONE;

        if (AudioSystem.isLineSupported(source)) 
        {
            try 
            {
                Port outline = (Port) AudioSystem.getLine(source);
                outline.open();                
                FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);                
                System.out.println("       volume: " + volumeControl.getValue() );
                float v = 0.33F;
                volumeControl.setValue(v);
                System.out.println("   new volume: " + volumeControl.getValue() );
                v = 0.73F;
                volumeControl.setValue(v); 
                System.out.println("newest volume: " + volumeControl.getValue() );
            } 
            catch (LineUnavailableException ex) 
            {
                System.err.println("source not supported");
                ex.printStackTrace();
            }            
        }
    } 

 }

这就是我在此应用程序中实现音量控制的方式:

http://onebeartoe.com/media-players/randomjuke/filesystem/java-web-start/swing/index.jsp

请注意,我只在 MS Windows 操作系统机器上有过使用它的经验。

Another (non-JLayer) way to adjust the PC's speaker volume is to use classes in the javax.sound.sampled package provided by Sun/Oracle.

On my machine, SPEAKER was the only output target supported.

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Port;

public class SoundVolumeDemo 
{

    public static void main(String[] args) 
{        
    Info source = Port.Info.SPEAKER;
    //        source = Port.Info.LINE_OUT;
    //        source = Port.Info.HEADPHONE;

        if (AudioSystem.isLineSupported(source)) 
        {
            try 
            {
                Port outline = (Port) AudioSystem.getLine(source);
                outline.open();                
                FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);                
                System.out.println("       volume: " + volumeControl.getValue() );
                float v = 0.33F;
                volumeControl.setValue(v);
                System.out.println("   new volume: " + volumeControl.getValue() );
                v = 0.73F;
                volumeControl.setValue(v); 
                System.out.println("newest volume: " + volumeControl.getValue() );
            } 
            catch (LineUnavailableException ex) 
            {
                System.err.println("source not supported");
                ex.printStackTrace();
            }            
        }
    } 

 }

This is how I am achieving volume control in this app:

http://onebeartoe.com/media-players/randomjuke/filesystem/java-web-start/swing/index.jsp

Please note that I have only had experience with it working on MS Windows OS machines.

橘虞初梦 2024-07-21 15:15:01

尝试调整均衡器的所有频段以获得您想要的音量。 也许您还可以使用 EQFunction< /a> 为此?

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

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); }
}
温柔戏命师 2024-07-21 15:15:01

我根据 ChrisH 的答案制作了自己的版本,但我没有更改 JLayer 的源代码,而是通过反射完成的:

Class<JavaSoundAudioDevice> clazz = JavaSoundAudioDevice.class;
Field[] fields = clazz.getDeclaredFields();

try{
    SourceDataLine source = null;
        for(Field field : fields) {
            if("source".equals(field.getName())) {
                field.setAccessible(true);
                source = (SourceDataLine) field.get(device);
                field.setAccessible(false);

                FloatControl volControl = (FloatControl) source.getControl(FloatControl.Type.MASTER_GAIN);
                if (volControl != null) {
                     float newGain = Math.min(Math.max(gain, volControl.getMinimum()), volControl.getMaximum());
                     volControl.setValue(newGain);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

有点粗糙,但它有效

I made my own version on ChrisH's answer, but instead of changing the source code of JLayer, I did it via reflection:

Class<JavaSoundAudioDevice> clazz = JavaSoundAudioDevice.class;
Field[] fields = clazz.getDeclaredFields();

try{
    SourceDataLine source = null;
        for(Field field : fields) {
            if("source".equals(field.getName())) {
                field.setAccessible(true);
                source = (SourceDataLine) field.get(device);
                field.setAccessible(false);

                FloatControl volControl = (FloatControl) source.getControl(FloatControl.Type.MASTER_GAIN);
                if (volControl != null) {
                     float newGain = Math.min(Math.max(gain, volControl.getMinimum()), volControl.getMaximum());
                     volControl.setValue(newGain);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Kinda crude, but it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文