feat: basic music backend
This commit is contained in:
40
src/music/player.rs
Normal file
40
src/music/player.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user