-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundPlayer.java
More file actions
43 lines (35 loc) · 1.24 KB
/
SoundPlayer.java
File metadata and controls
43 lines (35 loc) · 1.24 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
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class SoundPlayer {
// constructor to initialize streams and clip
public void play(String fileName) throws MultimediaException {
// create AudioInputStream object
try {
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(new File(fileName).getAbsoluteFile());
// create clip reference
Clip clip = AudioSystem.getClip();
// open audioInputStream to the clip
clip.open(audioInputStream);
clip.start();
System.out.print("Press RET to continue");
BufferedReader keyboard = new BufferedReader
(new InputStreamReader(System.in));
String c = keyboard.readLine();
stop(clip);
}
catch (Exception e) {
throw new MultimediaException("Error processing input file "+fileName);
}
}
// Method to stop the audio
public void stop(Clip clip) throws UnsupportedAudioFileException,
IOException, LineUnavailableException {
clip.stop();
clip.close();
}
}