You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

  1. package playground;
  2. import java.io.File;
  3. import javax.sound.sampled.AudioInputStream;
  4. import javax.sound.sampled.AudioSystem;
  5. import javax.sound.sampled.Clip;
  6. import javax.sound.sampled.FloatControl;
  7. public class Music {
  8. private static final float volume = 0.02f; // scale 0 silence, 1 no change, 2 double. (linear).
  9. public static synchronized void music(File track) {
  10. final File trackname = track;
  11. new Thread(new Runnable() {
  12. @Override
  13. public void run() {
  14. while (true) {
  15. try {
  16. Clip clip = AudioSystem.getClip();
  17. AudioInputStream inputstream = AudioSystem.getAudioInputStream(trackname);
  18. clip.open(inputstream);
  19. FloatControl volumeControl =
  20. (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
  21. if (volumeControl != null) {
  22. volumeControl.setValue(20f * (float) Math.log10(volume));
  23. }
  24. clip.start();
  25. Thread.sleep(clip.getMicrosecondLength() / 1);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. }).start();
  32. }
  33. }