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
use soloud::SoloudError;
use soloud::audio::Wav;
use soloud::Soloud;
use soloud::Handle;
pub mod nonblocking {
// use super::SoloudError;
// use super::Wav;
// use super::Soloud;
// use super::Handle;
use soloud::*;
// TODO this is broken if more than 1 instance of this struct is alive
#[derive(Debug)]
pub struct MusicPlayer {
pub player: Soloud,
wav: Wav,
playing: bool,
// songs: HashMap<&'a str, >
handle: Option<Handle>,
}
impl MusicPlayer {
pub fn new() -> Result<Self, SoloudError> {
let mut sl = Soloud::default()?;
// sl.set_global_volume(2.0);
let mut wav = Wav::default();
Ok(Self {
player: sl,
wav,
playing: false,
handle: None,
})
}
pub fn load_file<P: AsRef<std::path::Path>>(
&mut self,
path: P,
) -> Result<(), SoloudError> {
self.wav.load(path)?;
Ok(())
}
pub fn pause_playing(&mut self) {
if let Some(handle) = self.handle {
self.playing = false;
self.player.set_pause(handle, true);
}
}
pub fn continue_playing(&mut self) {
if let Some(handle) = self.handle {
self.playing = true;
self.player.set_pause(handle, false);
}
}
pub fn is_done_playing(&self) -> bool {
self.player.voice_count() == 0
}
pub fn is_playing(&self) -> bool {
self.player.voice_count() > 0
}
pub fn play_music(&mut self, wait: bool, timeout: Option<u64>) {
self.playing = true;
let handle = self.player.play(&self.wav);
self.handle = Some(handle);
if wait {
// calls to play are non-blocking, so we put the thread to sleep
while self.is_playing() {
if let Some(timeout) = timeout {
std::thread::sleep(std::time::Duration::from_millis(
timeout,
));
}
}
}
}
}
}
// // mod blocking {
// // pub fn playsound()
// // }