[英]Changing volume in Java when using JLayer
我正在使用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();
您可能會認為這是一個不可接受的黑客,但它在我寫的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);
}
調整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操作系統計算機上工作的經驗。
我在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();
}
}
有點原油,但它的工作原理
嘗試調整均衡器的所有波段,以獲得您想要獲得的音量。 也許你也可以使用EQFunction ?
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); }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.