使用 JLayer 调整音量 [英] Adjusting Volume using JLayer

查看:35
本文介绍了使用 JLayer 调整音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一个朋友正在编写 MP3 播放器作为学校项目.我们快完成了,现在卡在我们尝试编写一个函数来改变播放器音量的地方.我们正在使用:

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:

  • 音频设备
  • 高级播放器

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

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.

干杯蒂莫西

推荐答案

最简单的方法是通过 mp3spi 这基本上意味着您通过 JavaSound 使用 jlayer.然后,您可以像在 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

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

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

其次,您需要在播放前解码 AudioInputStream.

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

然后播放解码后的流:

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

和 JavaSound API 控件可用:

and JavaSound API controls are available:

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

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

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.

这篇关于使用 JLayer 调整音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆