-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecialisedSound.java
More file actions
61 lines (57 loc) · 1.86 KB
/
SpecialisedSound.java
File metadata and controls
61 lines (57 loc) · 1.86 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
//Items to play sound
import java.io.*;
import sun.audio.*;
//To use array lists
import java.util.*;
public class SpecialisedSound{
AudioStream as;
long duration, maxDuration;
boolean isPlaying = false;
String musicPlaying;
FileInputStream fs;
boolean isRestarting = false;
void playSound(String music, boolean canPlayMusic){
musicPlaying = music;
if(canPlayMusic == true){
try{
FileInputStream fs = new FileInputStream(music);
as = new AudioStream(fs);
AudioPlayer.player.start(as);
duration = System.currentTimeMillis();
isPlaying = true;
}catch (Exception e){
System.err.println(e);
}
}
}
void playSound(ArrayList<String> music, boolean canPlayMusic){
if(canPlayMusic == true){
try{
int i = (int)(Math.random()*(music.size()));
fs = new FileInputStream(music.get(i));
as = new AudioStream(fs);
AudioPlayer.player.start(as);
duration = System.currentTimeMillis();
isPlaying = true;
}catch (Exception e){
System.err.println(e);
}
}
}
void stopSound(){
AudioPlayer.player.stop(as);
}
void checkForLoop(boolean canPlayMusic){
if(isPlaying && System.currentTimeMillis() - duration > maxDuration && canPlayMusic == true && isRestarting == false){
isRestarting = true;
stopSound();
duration = System.currentTimeMillis();
try{
fs = new FileInputStream(musicPlaying);
as = new AudioStream(fs);
}catch(Exception e){}
AudioPlayer.player.start(as);
isRestarting = false;
}
}
}