[英]Adjusting Volume using JLayer
me and a friend are programming a MP3 Player as a schoolproject. 我和一个朋友正在将MP3播放器编程为学校项目。 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: 我们正在使用:
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 干杯蒂莫西
The easiest way to do this is to use jlayer via mp3spi which basically means you use jlayer via JavaSound. 最简单的方法是通过mp3spi使用jlayer,这基本上意味着您通过JavaSound使用jlayer。 You can then set gain on the line as you would in JavaSound. 然后,您可以像在JavaSound中一样在线路上设置增益。
Firstly, you will need to add the following to your classpath: 首先,您需要将以下内容添加到类路径中:
...all of which are in the distribution for mp3spi (linked above). ...所有这些都在mp3spi的发行版中(上面链接)。
Secondly, you will need to decode the AudioInputStream prior to playback. 其次,您需要在播放之前解码AudioInputStream。
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: 和JavaSound API控件可用:
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 . 注意:不要忘记关闭您的资源,我刚刚展示了此问题的关键点-应该熟悉JavaSound, 请在此处阅读 。
JLGUI is a good example of a UI-based JLayer app adjusting volume. JLGUI是基于UI的JLayer应用程序调整音量的一个很好的例子。 You can get the source code in the tar.gz file. 您可以在tar.gz文件中获取源代码。 http://www.javazoom.net/jlgui/sources.html 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.