157 lines
4.0 KiB
Rust
157 lines
4.0 KiB
Rust
///Contains everything needed to play music.
|
|
///The player is akin to a virtual cassette player, where the audio files would be cassettes themselves,
|
|
///stored inside the playlist.
|
|
pub mod audioplayer {
|
|
|
|
use std::fs::File;
|
|
use std::io::BufReader;
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::Duration;
|
|
use rodio::{Decoder, OutputStream, Sink, Source};
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use crate::playlist::playlist::*;
|
|
use crate::songmeta::songmeta::*;
|
|
use std::io::{Read, Seek};
|
|
|
|
//-----------------------------------Errors
|
|
#[derive(Debug, Clone)]
|
|
pub enum PlayerError {
|
|
Something,
|
|
}
|
|
|
|
///Main error container for the player.
|
|
impl fmt::Display for PlayerError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
PlayerError::Something => write!(f, "Uh-oh"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Error for PlayerError {}
|
|
|
|
|
|
//-----------------------------------------Structs
|
|
pub struct AudioPlayer {
|
|
///A facility for controlling the playing of sound.
|
|
pub sink: Sink,
|
|
///A handle to the sound device. Must live as long as the sink.
|
|
stream: OutputStream,
|
|
///List of songs to pick from.
|
|
pub playlist: Playlist,
|
|
///If true, the player will loop back to the beginning of the playlist when it's done
|
|
pub repeating: bool,
|
|
///Stores the current state of the player, can be used as a source of truth for an UI.
|
|
pub state: PlayerState,
|
|
|
|
}
|
|
|
|
//-----------------------------------Enums
|
|
#[derive(Debug)]
|
|
pub enum PlayerState {
|
|
Playing,
|
|
Paused,
|
|
Stopped,
|
|
}
|
|
|
|
//-------------------------------------Impls
|
|
|
|
|
|
|
|
|
|
impl AudioPlayer {
|
|
|
|
///Returns a new empty player to the default device.
|
|
pub fn new() -> Result<Self, Box<dyn Error>> {
|
|
let (stream, stream_handle) = OutputStream::try_default()?;
|
|
let sink = Sink::try_new(&stream_handle)?;
|
|
let playlist = Playlist::new();
|
|
|
|
let me = Self {
|
|
sink: sink,
|
|
stream: stream,
|
|
playlist: playlist,
|
|
repeating: false,
|
|
state: PlayerState::Stopped,
|
|
};
|
|
Ok(me)
|
|
}
|
|
|
|
///Plays all files in the playlist starting from index.
|
|
pub fn play_at(&mut self, index: usize) -> Result<(), Box<dyn Error>> {
|
|
self.playlist.check_index(index.clone())?;
|
|
self.sink.clear();
|
|
self.state = PlayerState::Paused;
|
|
for song in &self.playlist.songs[index..] {
|
|
let file = BufReader::new(File::open(&song.path)?);
|
|
let source = Decoder::new(file)?;
|
|
self.sink.append(source);
|
|
}
|
|
self.sink.play();
|
|
self.state = PlayerState::Playing;
|
|
return Ok(());
|
|
}
|
|
|
|
fn get_current_song(&self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn skip_forward(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn skip_backwards(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn pause(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn unpause(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn get_pos(&self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn set_vol(&mut self, vol: f32) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn get_vol(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn goto(&mut self, index: usize) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn press_shuffle(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn press_repeat(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn seek(&mut self, goto: Duration) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn stop(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn load_file(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn load_folder(&mut self) {
|
|
unimplemented!();
|
|
}
|
|
}
|
|
}
|