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.

47 lines
1002 B

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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 = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
  20. if (volumeControl != null) {
  21. volumeControl.setValue(20f * (float) Math.log10(volume));
  22. }
  23. clip.start();
  24. Thread.sleep(clip.getMicrosecondLength() / 1);
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. }).start();
  31. }
  32. }