Java中的音频音量控制(增加或减少)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/953598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Audio volume control (increase or decrease) in Java
提问by Cliff
How do I increase the volume of an outgoing wav audio stream using Java? I'm having issues with various Java TTS engines and the output volume of the synthesized speech. Is there an API call or a doo-hickey.jar I can use to pump up the volume?
如何使用 Java 增加传出 wav 音频流的音量?我在使用各种 Java TTS 引擎和合成语音的输出音量时遇到问题。我可以使用 API 调用或 doo-hickey.jar 来提高音量吗?
采纳答案by markusk
If you're using the Java Sound API, you can set the volume with the MASTER_GAINcontrol.
如果您使用的是 Java Sound API,则可以使用MASTER_GAIN控件设置音量。
import javax.sound.sampled.*;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
new File("some_file.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-10.0f); // Reduce volume by 10 decibels.
clip.start();
回答by objects
You can adjust volume using a GainControl, try something like this after you have opened the line
您可以使用 GainControl 调整音量,打开线路后尝试类似的操作
FloatControl volume= (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
回答by Jan Cajthaml
public final class VolumeControl
{
private VolumeControl(){}
private static LinkedList<Line> speakers = new LinkedList<Line>();
private final static void findSpeakers()
{
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for (Mixer.Info mixerInfo : mixers)
{
if(!mixerInfo.getName().equals("Java Sound Audio Engine")) continue;
Mixer mixer = AudioSystem.getMixer(mixerInfo);
Line.Info[] lines = mixer.getSourceLineInfo();
for (Line.Info info : lines)
{
try
{
Line line = mixer.getLine(info);
speakers.add(line);
}
catch (LineUnavailableException e) { e.printStackTrace(); }
catch (IllegalArgumentException iaEx) { }
}
}
}
static
{
findSpeakers();
}
public static void setVolume(float level)
{
System.out.println("setting volume to "+level);
for(Line line : speakers)
{
try
{
line.open();
FloatControl control = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue(limit(control,level));
}
catch (LineUnavailableException e) { continue; }
catch(java.lang.IllegalArgumentException e) { continue; }
}
}
private static float limit(FloatControl control,float level)
{ return Math.min(control.getMaximum(), Math.max(control.getMinimum(), level)); }
}