-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundDriver.java
More file actions
executable file
·85 lines (73 loc) · 2.63 KB
/
Copy pathSoundDriver.java
File metadata and controls
executable file
·85 lines (73 loc) · 2.63 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.sound.sampled.*;
import java.io.*;
/**
* @(#)SoundDriver.java
*
* @author David Navarro, Nicholas Hernandez
* @version 1.01 5/6/2014
*
*/
public class SoundDriver {
private Clip[] clips;
private int[] framePosition;
private boolean[] isPlaying;
private FloatControl gainControl;
public SoundDriver(String[] aClips) {
clips = new Clip[aClips.length];
framePosition = new int[aClips.length];
isPlaying = new boolean[aClips.length];
try {
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED,
16, 2, 4,
AudioSystem.NOT_SPECIFIED, true);
DataLine.Info info = new DataLine.Info(Clip.class, format);
for (int i = 0; i < clips.length; i++) {
File soundFile = new File(aClips[i]);
BufferedInputStream bs = new BufferedInputStream(new FileInputStream(soundFile));
AudioInputStream soundIn = AudioSystem.getAudioInputStream(bs);
clips[i] = (Clip) AudioSystem.getLine(info);
clips[i].open(soundIn);
gainControl = (FloatControl) clips[i].getControl(FloatControl.Type.MASTER_GAIN);
}
//System.out.println("Audio File Loaded");
} catch (UnsupportedAudioFileException ex) {
System.out.println("Unsupported Audio File");
} catch (LineUnavailableException ex) {
System.out.println("Line Unavailable");
} catch (IOException ex) {
System.out.println("IO Error" + ex);
}
}
public void play(int value) {
clips[value].stop();
clips[value].setFramePosition(0);
clips[value].start();
}
public void loop(int value) {
clips[value].stop();
clips[value].setFramePosition(0);
clips[value].loop(Clip.LOOP_CONTINUOUSLY);
}
public void stop(int value) {
clips[value].stop();
}
public void pause(int value){
framePosition[value] = clips[value].getFramePosition();
clips[value].stop();
}
public void resume(int value){
clips[value].setFramePosition(framePosition[value]);
clips[value].start();
}
public boolean isPlaying(int value){
return clips[value].isRunning();
}
public void setVolume(float volume) {
//gainControl.setValue(volume);
}
}