fix: musicplayer starting on launch
This commit is contained in:
parent
135b4f1697
commit
b8a56d9a2c
2 changed files with 64 additions and 24 deletions
50
src/main.rs
50
src/main.rs
|
@ -89,7 +89,6 @@ fn main() {
|
||||||
|
|
||||||
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
|
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
|
||||||
let mut music_player = MusicPlayer::new(test_file[0].clone(), &stream_handle);
|
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();
|
let server = TcpListener::bind("127.0.0.1:9001").unwrap();
|
||||||
|
|
||||||
|
@ -103,21 +102,37 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if sockets.len() == 0 {
|
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() {
|
for i in 0..sockets.len() {
|
||||||
if let Ok(mess) = sockets[i].read_message() {
|
println!("{}", sockets.len());
|
||||||
if mess.is_text() {
|
match sockets[i].read_message() {
|
||||||
match server_handling::handle_request(mess.into_text().unwrap()) {
|
Ok(mess) => {
|
||||||
Err(error) => {
|
if mess.is_text() {
|
||||||
println!("There was an error decoding the message: {:?}", error)
|
match server_handling::handle_request(mess.into_text().unwrap()) {
|
||||||
}
|
Err(error) => {
|
||||||
Ok(req) => {
|
println!("There was an error decoding the message: {:?}", error)
|
||||||
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 {
|
match items {
|
||||||
None => {
|
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();
|
.unwrap();
|
||||||
}
|
}
|
||||||
Some(items) => {
|
Some(items) => {
|
||||||
if items.len() > 1 {
|
if items.len() > 1 {
|
||||||
write_to_socket(socket, "Please be more specific".to_string(), items)
|
write_to_socket(
|
||||||
.unwrap();
|
socket,
|
||||||
|
"Multiple results found\nPlease be more specific".to_string(),
|
||||||
|
items,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
"Switching song to: '{}'",
|
"Switching song to: '{}'",
|
||||||
items.get(0).unwrap().title.clone()
|
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());
|
println!("{}", items.get(0).unwrap().path.clone());
|
||||||
|
|
||||||
write_to_socket(socket, "Switching now playing".to_string(), items)
|
write_to_socket(socket, "Switching now playing".to_string(), items)
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
|
//use rodio::decoder::DecoderError;
|
||||||
use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink};
|
use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
|
||||||
use crate::message_types::ItemTag;
|
use crate::message_types::ItemTag;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MusicPlayerError {
|
||||||
|
DecoderError,
|
||||||
|
IOError,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct MusicPlayer<'a> {
|
pub struct MusicPlayer<'a> {
|
||||||
output_stream_handle: &'a OutputStreamHandle,
|
output_stream_handle: &'a OutputStreamHandle,
|
||||||
playing_sink: rodio::Sink,
|
playing_sink: rodio::Sink,
|
||||||
|
@ -11,25 +18,21 @@ pub struct MusicPlayer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> MusicPlayer<'a> {
|
impl<'a> MusicPlayer<'a> {
|
||||||
pub fn new (starting_item: ItemTag, output_stream_handle: &'a OutputStreamHandle) -> Self {
|
pub fn new(starting_item: ItemTag, output_stream_handle: &'a OutputStreamHandle) -> Self {
|
||||||
|
|
||||||
let sink = Sink::try_new(&output_stream_handle).unwrap();
|
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 file = BufReader::new(File::open(starting_item.path.clone()).unwrap());
|
||||||
|
|
||||||
let source = Decoder::new(file).unwrap();
|
let source = Decoder::new(file).unwrap();
|
||||||
sink.append(source);
|
sink.append(source);
|
||||||
|
|
||||||
//mp.playing_sink.play();
|
|
||||||
//mp.playing_sink.sleep_until_end();
|
|
||||||
|
|
||||||
//mp.pause();
|
|
||||||
let mp = MusicPlayer {
|
let mp = MusicPlayer {
|
||||||
output_stream_handle,
|
output_stream_handle,
|
||||||
playing_sink: sink,
|
playing_sink: sink,
|
||||||
currently_playing: starting_item,
|
currently_playing: starting_item,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mp.pause();
|
||||||
return mp;
|
return mp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,10 +46,25 @@ impl<'a> MusicPlayer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: set these to return results
|
// TODO: set these to return results
|
||||||
pub fn change_now_playing(self: &mut Self, item: ItemTag) {
|
pub fn change_now_playing(self: &mut Self, item: ItemTag) -> Result<(), MusicPlayerError> {
|
||||||
let source = Decoder::new(BufReader::new(File::open(item.path.clone()).unwrap())).unwrap();
|
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.stop();
|
||||||
self.playing_sink = Sink::try_new(self.output_stream_handle).unwrap();
|
self.playing_sink = Sink::try_new(self.output_stream_handle).unwrap();
|
||||||
self.playing_sink.append(source);
|
self.playing_sink.append(source.unwrap());
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue