Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look here: <a href="http://potatoland.com/openjava/demo/Sound.java" rel="nofollow noreferrer">Openjava sound demo</a><br> They use <code>FloatControl gainControl; // for volume</code><br></p> <pre><code>/** * Set the volume to a value between 0 and 1. */ public void setVolume(double value) { // value is between 0 and 1 value = (value&lt;=0.0)? 0.0001 : ((value&gt;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&lt;=0.0)? 0.0001 : ((value&gt;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 &gt; targetDB) { while (currDB &gt; targetDB) { currDB -= fadePerStep; gainControl.setValue(currDB); try {Thread.sleep(10);} catch (Exception e) {} } } else if (currDB &lt; targetDB) { while (currDB &lt; targetDB) { currDB += fadePerStep; gainControl.setValue(currDB); try {Thread.sleep(10);} catch (Exception e) {} } } fading = false; currDB = targetDB; // now sound is at this volume level } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload