feat: basic music backend

This commit is contained in:
2024-11-24 00:10:56 +01:00
commit 51a57ebd40
23 changed files with 4091 additions and 0 deletions

40
src/music/player.rs Normal file
View File

@@ -0,0 +1,40 @@
use std::{
fs,
io::{BufReader, Cursor}, path::Path,
};
use rodio::{Decoder, Sink, Source};
pub struct AudioPlayer {
pub sink: Sink,
}
impl AudioPlayer {
pub fn new(sink: Sink) -> Self {
Self {
sink
}
}
pub fn play_song<P>(&mut self, path: P) -> Result<(), Box<dyn std::error::Error>> where P: AsRef<Path> {
self.sink.clear();
let file = BufReader::new(Cursor::new(fs::read(path)?));
let source = Decoder::new(file)?.amplify(0.2);
self.sink.append(source);
self.sink.play();
Ok(())
}
pub fn resume(&mut self) {
self.sink.play();
}
pub fn pause(&mut self) {
self.sink.pause();
}
}