java声音淡出

2023-11-30

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

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

谁能指出我该如何做到这一点?我应该使用不同的声音系统,例如 FMOD(如果这在 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java声音淡出 的相关文章

随机推荐

  • pip 缓存文件夹在哪里?

    Python pip 缓存文件夹在哪里 我在安装过程中遇到错误 现在使用缓存文件重新安装软件包 那个目录在哪里 我想对它们进行备份以便将来安装 是否可以 例如我有这个 Using cached cssselect 0 9 1 tar gz
  • 如何将 C++ 字符串转换为 int? [复制]

    这个问题在这里已经有答案了 可能的重复 C 将字符串转换为int 我让用户按顺序输入 9 个数字 我需要将字符串数字转换为 int string num int num int product 10 cout lt lt enter num
  • Magento 前端(会话)cookie

    我在我的 magento 商店的 IE 中出现一些奇怪的行为 丢失了前端 会话 cookie 有人知道在magento代码中前端cookie的设置在哪里吗 Thanks 阿法克 frontend cookie 在分派当前操作之前设置 看一下
  • VB6 中未设置变量的默认值是多少?

    我正在使用一些旧代码 它定义了一个像这样的全局变量 Public myvar 该变量从未被赋值 但稍后在测试中使用 If myvar lt gt somevalue then do something End If 为了某种价值的什么价值
  • X86 编码近调用相对偏移量

    假设我有以下一组说明 00E79E00 E8 AE580000 CALL someprocess 00E7F6B3 00E79E05 85C0 TEST EAX EAX output taken from OllyDbg 如何对近调用 0x
  • [Int] 和 Array 有什么区别?

    let numberList Array 1 10 type Array
  • 方法名称必须以 findBy 或 findOneBy 开头。 Symfony 未定义方法?

    我正在努力通过Symfony2 的第 4 部分 在更新控制器和帮助程序类代码时 我收到以下错误消息 Undefined method getLatestBlogs The method name must start with either
  • 编译时在浮点型和双精度型之间切换

    如果我想在编译时在浮点精度和双精度之间切换 我应该在哪里查看 就像 如果用户想要所有内容都是浮点数而不是双精度 我如何保持这种灵活性 换句话说 我应该如何定义一个可以有条件地浮点或双精度的变量 如果可以在编译时进行切换 一个简单的方法typ
  • 为什么内联 div 的行为与内联 span 不同

    我尝试过内联 div 但它不起作用 很难解释 请看下面的链接 http jsfiddle net CsS5v 1 p style background red SDFDSDSFDSSFDAFASasf br span style backg
  • PHP:如何重命名方法?

    PHP 5 2 中是否可以在运行时重命名类方法 是否可以使用反射来做到这一点 Given class Test public function myMethod echo in my method 我希望能够重命名myMethod to o
  • ng-include 和 ngRoute:如何让它们一起工作? (即路由到 ng-include 中的视图)

    EDITED 我的应用程序具有以下结构 索引 html
  • 如何在 C 中进行 Base64 编码(解码)?

    我在 unsigned char 变量中有二进制数据 我需要在 c 中将它们转换为 PEM base64 我查看了 openssl 库 但找不到任何函数 有谁有任何想法吗 这是我正在使用的 include
  • 静态局部变量什么时候出现?

    这本书Robert Lafore 的 C 面向对象编程 says 静态局部变量具有自动局部变量的可见性 变量 即在包含它的函数内部 然而 其 生命周期与全局变量的生命周期相同 只是它 直到第一次调用该函数时才存在 包含它 此后它在人的一生中
  • 将 ItemsControl ItemSource 绑定到 UserControl 依赖属性

    这是我第一次尝试创建具有依赖属性的用户控件 所以请原谅我对这个主题缺乏了解 我在其中一个页面上创建了一个总体设计 我想将其转换为可重用的用户控件 页面上的原始控件 这就是我试图移植到可重用 UserControl 的控件
  • 比较 2 个对象 PHP

    我需要比较 2 个对象以删除重复项 查找新条目 这些对象不相同 但它们包含相同的用户名密钥 这是布局 数据库对象 array 0 db gt username 0 db gt something 1 db gt username 1 db
  • 如何将多个 LineString 行组合成一个单行集合

    我正在使用 SQL Server 2008 和Geometry数据类型来存储英国道路列表 我从地形测量战略数据集 每条路被分成多行 每行包含一行 A Linestring由一段组成 例如A369由18条独立的线路组成 如下图所示 我想要做的
  • Angular 9 路线给出空白页面,没有错误

    我刚刚升级到 Angular 9 并使用延迟加载创建了一些子路由 在这些更改之前 我的项目和我的路线运行得很好 但在这两个更改之后 我的项目和路线工作得很好 HomeComponent路线 路径 工作正常 但如果我尝试单击其他路线的链接 使
  • 批处理:在我的代码循环期间,它停止设置变量。欢迎大家帮忙

    goto time time set tm time set hh tm 0 2 set mm tm 3 2 set ss tm 6 2 set ms tm 7 2 goto date date set dt date set wd dt
  • 如何使用C#获取Win7的SSID和RSSI

    我对Win7和WMI很陌生 请告诉我在哪里可以看到 WiFi 的活动接入点以及如何获取每个接入点的 ssid rssi 我有使用 ManagementClass mc new ManagementClass root WMI MSNdis
  • java声音淡出

    使用 javax sound sampled 我想淡出我开始无限循环的声音 这就是我开始发出声音的方式 Clip clip AudioSystem getClip AudioInputStream inputStream AudioSyst