-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundClipTest.java
54 lines (42 loc) · 1.55 KB
/
SoundClipTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package mao;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class SoundClipTest {
public static void main(String filename) {
new SoundClipTest(filename);
}
public SoundClipTest(String filename) {
try {
Scanner x = new Scanner(new File("src/userdata/currentaccount.txt"));
Scanner in = new Scanner(new File("src/userdata/" + x.nextLine() + ".txt"));
in.nextLine(); in.nextLine(); in.nextLine();
float volume = Float.parseFloat(in.nextLine())/100;
in.reset();
File soundFile = new File(filename);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
float range = gainControl.getMaximum() - gainControl.getMinimum();
float gain = (range * volume) + gainControl.getMinimum();
gainControl.setValue(gain);
if(volume == 0) {
gainControl.setValue(gainControl.getMinimum());
}
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}