fix: musicplayer starting on launch

This commit is contained in:
Nickiel12 2022-12-24 16:34:20 -08:00
parent 135b4f1697
commit b8a56d9a2c
2 changed files with 64 additions and 24 deletions

View file

@ -89,7 +89,6 @@ fn main() {
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
let mut music_player = MusicPlayer::new(test_file[0].clone(), &stream_handle);
music_player.play();
let server = TcpListener::bind("127.0.0.1:9001").unwrap();
@ -103,21 +102,37 @@ fn main() {
}
if sockets.len() == 0 {
std::thread::sleep(std::time::Duration::from_secs(1));
std::thread::sleep(std::time::Duration::from_millis(200));
}
// Need to get an asynchronous socket reader like tokio
for i in 0..sockets.len() {
if let Ok(mess) = sockets[i].read_message() {
println!("{}", sockets.len());
match sockets[i].read_message() {
Ok(mess) => {
if mess.is_text() {
match server_handling::handle_request(mess.into_text().unwrap()) {
Err(error) => {
println!("There was an error decoding the message: {:?}", error)
}
Ok(req) => {
handle_uirequest(req, &mut sockets[i], &mut music_player, &dbo, &stream_handle).unwrap()
Ok(req) => handle_uirequest(
req,
&mut sockets[i],
&mut music_player,
&dbo,
&stream_handle,
)
.unwrap(),
}
}
}
Err(error) => {
if error.to_string().starts_with("Connection closed normally") {
sockets.remove(i);
} else {
println!("a socket errored: {}", error.to_string());
}
}
}
}
}
@ -168,19 +183,26 @@ fn handle_uirequest(
match items {
None => {
write_to_socket(socket, "No song found with that title!".to_string(), vec![])
write_to_socket(socket, "No song found with that field!".to_string(), vec![])
.unwrap();
}
Some(items) => {
if items.len() > 1 {
write_to_socket(socket, "Please be more specific".to_string(), items)
write_to_socket(
socket,
"Multiple results found\nPlease be more specific".to_string(),
items,
)
.unwrap();
} else {
println!(
"Switching song to: '{}'",
items.get(0).unwrap().title.clone()
);
music_player.change_now_playing(items.get(0).unwrap().clone());
music_player
.change_now_playing(items.get(0).unwrap().clone())
.unwrap();
println!("{}", items.get(0).unwrap().path.clone());
write_to_socket(socket, "Switching now playing".to_string(), items)

View file

@ -1,9 +1,16 @@
//use rodio::decoder::DecoderError;
use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink};
use std::fs::File;
use std::io::BufReader;
use crate::message_types::ItemTag;
#[derive(Debug)]
pub enum MusicPlayerError {
DecoderError,
IOError,
}
pub struct MusicPlayer<'a> {
output_stream_handle: &'a OutputStreamHandle,
playing_sink: rodio::Sink,
@ -12,24 +19,20 @@ pub struct MusicPlayer<'a> {
impl<'a> MusicPlayer<'a> {
pub fn new(starting_item: ItemTag, output_stream_handle: &'a OutputStreamHandle) -> Self {
let sink = Sink::try_new(&output_stream_handle).unwrap();
//println!("filepath: {}", mp.currently_playing.path.clone());
let file = BufReader::new(File::open(starting_item.path.clone()).unwrap());
let source = Decoder::new(file).unwrap();
sink.append(source);
//mp.playing_sink.play();
//mp.playing_sink.sleep_until_end();
//mp.pause();
let mp = MusicPlayer {
output_stream_handle,
playing_sink: sink,
currently_playing: starting_item,
};
mp.pause();
return mp;
}
@ -43,10 +46,25 @@ impl<'a> MusicPlayer<'a> {
}
// TODO: set these to return results
pub fn change_now_playing(self: &mut Self, item: ItemTag) {
let source = Decoder::new(BufReader::new(File::open(item.path.clone()).unwrap())).unwrap();
pub fn change_now_playing(self: &mut Self, item: ItemTag) -> Result<(), MusicPlayerError> {
println!("\n\n switching now playing to: {}", item.path.clone());
let file = File::open(item.path.clone());
if file.is_err() {
return Err(MusicPlayerError::IOError);
}
let reader = BufReader::new(file.unwrap());
let source = Decoder::new(reader);
if source.is_err() {
return Err(MusicPlayerError::DecoderError);
}
self.playing_sink.stop();
self.playing_sink = Sink::try_new(self.output_stream_handle).unwrap();
self.playing_sink.append(source);
self.playing_sink.append(source.unwrap());
Ok(())
}
}