-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundPlayer.java
More file actions
43 lines (36 loc) · 1.33 KB
/
Copy pathSoundPlayer.java
File metadata and controls
43 lines (36 loc) · 1.33 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 javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
public class SoundPlayer {
/**
* Used to create a sound file
*/
public static void voice(String fileName) throws MalformedURLException, LineUnavailableException, UnsupportedAudioFileException, IOException {
File url = new File(fileName);
if (!url.exists()) {
System.err.println("Sound file not found: " + fileName);
return;
}
Clip clip = AudioSystem.getClip();
try (AudioInputStream ais = AudioSystem.getAudioInputStream(url)) {
clip.open(ais);
clip.start();
// Wait for the sound to finish playing before returning (optional)
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
e.printStackTrace();
System.err.println("Error playing sound: " + fileName);
}
}
/**
* Method to play sound effect for bouncing
*/
public static void playSound(String path) {
try {
voice(path);
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
// Handle the specific sound exception here
e.printStackTrace();
}
}
}