/
SoundManager.pde
78 lines (66 loc) · 1.69 KB
/
SoundManager.pde
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright 2017 Billy Brown
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import ddf.minim.*;
import java.util.HashMap;
import java.util.Map;
// A simple wrapper around minim.AudioPlayer
class Sound {
private final AudioPlayer player;
private boolean playing;
public Sound(final AudioPlayer player) {
this.player = player;
playing = false;
}
public void start() {
// Always start from the beginning
player.rewind();
player.play();
playing = true;
}
public void stop() {
// Always return to the beginning
player.pause();
player.rewind();
playing = false;
}
public boolean isPlaying() {
// The song may have ended on its own
playing = playing && player.isPlaying();
return playing;
}
}
class SoundManager {
private Minim minim;
// Sounds are given a name
private Map<String, Sound> sounds;
public SoundManager(Object that) {
minim = new Minim(that);
sounds = new HashMap<String, Sound>();
}
public void addSound(final String file, final String name) {
sounds.put(name, new Sound(minim.loadFile(file)));
}
public void start(final String name) {
final Sound sound = sounds.get(name);
if (sound != null) {
sound.start();
}
}
public void stop(final String name) {
final Sound sound = sounds.get(name);
if (sound != null) {
sound.stop();
}
}
public boolean isPlaying(final String name) {
final Sound sound = sounds.get(name);
if (sound != null) {
return sound.isPlaying();
}
return false;
}
}