java的声音淡出 [英] java sound fade out

查看:152
本文介绍了java的声音淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用javax.sound.sampled中,我想淡出的声音,我开始无限循环。我就是这样开始的声音:

Using javax.sound.sampled, I want to fade out a sound I started looping infinitely. This is how I started the sound:

Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(new File("x.wav"));
clip.open(inputStream);
clip.loop(clip.LOOP_CONTINUOUSLY);

任何人都可以指出我我该怎么办呢?我应该使用不同的音效系统,如FMOD(如果这是可能在Java中)?谢谢你。

Could anyone point me to how I can do this? Should I be using a different sound system, such as FMOD (if this is possible in Java)? Thanks.

推荐答案

看看这里: Openjava声音演示结果
他们使用 FloatControl gainControl; //音量搜索

/**
 * 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
}

这篇关于java的声音淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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