使用 JLayer 调整音量

发布于 2024-09-07 17:42:28 字数 210 浏览 6 评论 0原文

我和一个朋友正在将 MP3 播放器编程作为学校项目。我们即将完成,现在陷入了尝试编写一个函数来改变播放器音量的阶段。 我们正在使用:

  • AudioDevice
  • AdvancedPlayer

我知道其他人已经问了同样的问题,但我没有完全得到解决方案,我不想回答这么老的问题,所以我想我只是再问一次。

干杯 蒂莫西

me and a friend are programming a MP3 Player as a schoolproject. 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:

  • AudioDevice
  • AdvancedPlayer

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

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

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

发布评论

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

评论(2

贵在坚持 2024-09-14 17:42:28

最简单的方法是通过 mp3spi 使用 jlayer,这基本上意味着您通过Java声音。然后您可以像在 JavaSound 中一样设置线路增益。

首先,您需要将以下内容添加到类路径中:

  • jl1.0.1.jar
  • mp3spi1.9.5.jar
  • tritonus_share.jar

...所有这些都在 mp3spi 的发行版中(上面链接)。

其次,您需要在播放之前对 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);

然后播放解码后的流:

Clip clip = AudioSystem.getClip();
clip.open(audioStream2);

并且 JavaSound API 控件可用:

FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-30.0f);

注意:不要忘记关闭资源,我刚刚展示了此问题的要点 - 需要熟悉 JavaSound,阅读此处

The easiest way to do this is to use jlayer via mp3spi which basically means you use jlayer via JavaSound. You can then set gain on the line as you would in JavaSound.

Firstly, you will need to add the following to your classpath:

  • jl1.0.1.jar
  • mp3spi1.9.5.jar
  • tritonus_share.jar

...all of which are in the distribution for mp3spi (linked above).

Secondly, you will need to decode the AudioInputStream prior to playback.

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:

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.

长发绾君心 2024-09-14 17:42:28

JLGUI 是基于 UI 的 JLayer 应用程序调整音量的一个很好的示例。您可以在 tar.gz 文件中获取源代码。 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);
        }
    }

JLGUI is a good example of a UI-based JLayer app adjusting volume. You can get the source code in the tar.gz file. 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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文