java出声音,Java声音淡出

小编典典

在这里看看:Openjava声音演示

他们使用FloatControl gainControl; // for volume

/**

* Set the volume to a value between 0 and 1.

*/

public void setVolume(double value) {

// value is between 0 and 1

value = (value<=0.0)? 0.0001 : ((value>1.0)? 1.0 : value);

try {

float dB = (float)(Math.log(value)/Math.log(10.0)*20.0);

gainControl.setValue(dB);

} catch (Exception ex) {

ex.printStackTrace();

}

}

/**

* Fade the volume to a new value. To shift volume while sound is playing,

* ie. to simulate motion to or from an object, the volume has to change

* smoothly in a short period of time. Unfortunately this makes an annoying

* clicking noise, mostly noticeable in the browser. I reduce the click

* by fading the volume in small increments with delays in between. This

* means that you can't change the volume very quickly. The fade has to

* to take a second or two to prevent clicks.

*/

float currDB = 0F;

float targetDB = 0F;

float fadePerStep = .1F; // .1 works for applets, 1 is okay for apps

boolean fading = false;

public void shiftVolumeTo(double value) {

// value is between 0 and 1

value = (value<=0.0)? 0.0001 : ((value>1.0)? 1.0 : value);

targetDB = (float)(Math.log(value)/Math.log(10.0)*20.0);

if (!fading) {

Thread t = new Thread(this); // start a thread to fade volume

t.start(); // calls run() below

}

}

/**

* Run by thread, this will step the volume up or down to a target level.

* Applets need fadePerStep=.1 to minimize clicks.

* Apps can get away with fadePerStep=1.0 for a faster fade with no clicks.

*/

public void run()

{

fading = true; // prevent running twice on same sound

if (currDB > targetDB) {

while (currDB > targetDB) {

currDB -= fadePerStep;

gainControl.setValue(currDB);

try {Thread.sleep(10);} catch (Exception e) {}

}

}

else if (currDB < targetDB) {

while (currDB < targetDB) {

currDB += fadePerStep;

gainControl.setValue(currDB);

try {Thread.sleep(10);} catch (Exception e) {}

}

}

fading = false;

currDB = targetDB; // now sound is at this volume level

}

2020-11-19

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值