| 67 | } |
| 68 | |
| 69 | pub(crate) fn play_loop(&self, path: &str, volume: f32) -> Result<(), MediaError> { |
| 70 | if let Some(s) = self.sink.borrow_mut().take() { |
| 71 | s.stop(); |
| 72 | } |
| 73 | |
| 74 | let file = File::open(path).map_err(|e| MediaError::OpenFile { |
| 75 | path: path.to_string(), |
| 76 | source: e, |
| 77 | })?; |
| 78 | let source = Decoder::new(BufReader::new(file)) |
| 79 | .map_err(|e| MediaError::DecodeAudio { |
| 80 | path: path.to_string(), |
| 81 | source: e, |
| 82 | })? |
| 83 | .repeat_infinite(); |
| 84 | |
| 85 | let sink = Sink::try_new(&self.stream_handle)?; |
| 86 | sink.append(source); |
| 87 | sink.set_volume(volume); |
| 88 | sink.play(); |
| 89 | |
| 90 | *self.sink.borrow_mut() = Some(sink); |
| 91 | Ok(()) |
| 92 | } |
| 93 | |
| 94 | pub(crate) fn stop(&self) { |
| 95 | if let Some(s) = self.sink.borrow_mut().take() { |