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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use thread;
use Arc;
use *;
use StateMutex;
/// Plays a sound asynchronously, and sets the pointed state's audio_finished to true
/// once it finishes.
/// The behavior is undefined if multiple sounds are played at the same time with the
/// same state instance.
/// Plays a sound, blocking until it's finished.
// Holds a thread handle and forces a wait-sleep to the caller if a sound is already being played
// Not currently in use anywhere
/*
pub struct UniqueAudioPlayer {
handle: Option<thread::JoinHandle<()>>,
}
impl UniqueAudioPlayer {
pub fn new() -> UniqueAudioPlayer {
UniqueAudioPlayer {
handle: None,
}
}
pub fn play_sound(&mut self, path : &'static str) {
match self.handle {
Some(_) => { // Wait for the previous thread to finish
self.handle.take().unwrap().join().expect("Could not join spawned thread");
self.start_thread(path);
},
None => self.start_thread(path),
}
}
fn start_thread(&mut self, path : &'static str) {
self.handle = Some(thread::spawn(move || {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
// Load a sound from a file, using a path relative to Cargo.toml
let file = BufReader::new(File::open(path).unwrap());
// Decode that sound file into a source
let source = Decoder::new(file).unwrap();
sink.append(source);
// The sound plays in a separate thread. This call will block the current thread until the sink
// has finished playing all its queued sounds.
sink.sleep_until_end();
}))
}
}
*/