-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundPlayer.java
More file actions
32 lines (26 loc) · 964 Bytes
/
Copy pathSoundPlayer.java
File metadata and controls
32 lines (26 loc) · 964 Bytes
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
import java.io.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import javax.sound.sampled.*;
public class SoundPlayer {
public SoundPlayer() {}
public void play(String fileName) throws MultimediaException{
/* Play a file of type .wav or .mid. If the file cannot be played a
MultimediaException is thrown. */
try {
File file = new File(fileName);
if (!file.isFile()) throw new MultimediaException("Invalid file");
URL url = file.toURI().toURL();
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.print("Press RET to continue.");
BufferedReader keyboard = new BufferedReader
(new InputStreamReader(System.in));
String c = keyboard.readLine();
ac.stop();
} catch (Exception e) {
throw new MultimediaException("Error playing sound file "+fileName);
}
}
}