-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.java
52 lines (48 loc) · 1.66 KB
/
Sound.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
//Items to play sound
import java.io.*;
import sun.audio.*;
//To use array lists
import java.util.*;
public class Sound{
static AudioStream as;
long timerToPlaySound, timeToPlaySound = 180;
static void playSound(String music, boolean canPlayMusic){
if(canPlayMusic == true){
try{
FileInputStream fs = new FileInputStream(music);
as = new AudioStream(fs);
AudioPlayer.player.start(as);
}catch (Exception e){
System.err.println(e);
}
}
}
void playSoundWithTimeRestriction(ArrayList<String> music, boolean canPlayMusic){
if(canPlayMusic == true && System.currentTimeMillis() > timerToPlaySound){
try{
timerToPlaySound = System.currentTimeMillis() + timeToPlaySound;
int i = (int)(Math.random()*(music.size()));
FileInputStream fs = new FileInputStream(music.get(i));
as = new AudioStream(fs);
AudioPlayer.player.start(as);
}catch (Exception e){
System.err.println(e);
}
}
}
static void playSound(ArrayList<String> music, boolean canPlayMusic){
if(canPlayMusic == true){
try{
int i = (int)(Math.random()*(music.size()));
FileInputStream fs = new FileInputStream(music.get(i));
as = new AudioStream(fs);
AudioPlayer.player.start(as);
}catch (Exception e){
System.err.println(e);
}
}
}
static void stopSound(){
AudioPlayer.player.stop(as);
}
}