You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.1 KiB
89 lines
3.1 KiB
use std::io::Read;
|
|
|
|
pub struct Player {
|
|
pub sink: rodio::Sink,
|
|
pub stream_thread: std::thread::JoinHandle<()>,
|
|
pub stream_handle: rodio::OutputStreamHandle,
|
|
pub stream_source: String,
|
|
pub stream_exif: String,
|
|
pub volume: f32,
|
|
pub playing: bool
|
|
}
|
|
|
|
impl Player {
|
|
pub fn new(sink: rodio::Sink, stream_handle: rodio::OutputStreamHandle) -> Self {
|
|
Self {
|
|
sink,
|
|
stream_handle,
|
|
stream_thread: std::thread::spawn(|| {}),
|
|
stream_source: "https://radio.wownero.com/wow.ogg".to_owned(),
|
|
stream_exif: "".to_owned(),
|
|
volume: 100.0,
|
|
playing: false
|
|
}
|
|
}
|
|
|
|
pub fn default() -> Self {
|
|
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
|
|
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
|
|
Self::new(sink, stream_handle)
|
|
}
|
|
|
|
pub fn check_radio_stream(&self) -> bool {
|
|
let res: reqwest::blocking::Response = reqwest::blocking::get(self.stream_source.clone()).expect("request failed");
|
|
if res.status().is_success() {
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn start_radio_stream(&self) -> std::thread::JoinHandle<()> {
|
|
let url = self.stream_source.clone();
|
|
let t: Result<std::thread::JoinHandle<()>, std::io::Error> = std::thread::Builder::new().name("radio_stream".to_string()).spawn(move || {
|
|
let mut res: reqwest::blocking::Response = reqwest::blocking::get(url).expect("request failed");
|
|
let mut out: std::fs::File = std::fs::File::create(&crate::RADIO_STREAM).expect("failed to create file");
|
|
std::io::copy(&mut res, &mut out).expect("failed to copy content");
|
|
});
|
|
return t.unwrap()
|
|
}
|
|
|
|
pub fn wait_for_source() {
|
|
loop {
|
|
let r: Result<std::fs::File, std::io::Error> = std::fs::File::open(&crate::RADIO_STREAM);
|
|
if r.is_ok() {
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
return ()
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_secs(3));
|
|
}
|
|
}
|
|
|
|
pub fn get_song_meta(&self) -> String {
|
|
let file = std::fs::File::open(&crate::RADIO_STREAM);
|
|
match file {
|
|
Ok(mut file) => {
|
|
let mut buffer = [0u8; 384];
|
|
file.read_exact(&mut buffer).unwrap();
|
|
let s = std::string::String::from_utf8_lossy(&buffer);
|
|
let re = regex::Regex::new(r"title=(.*).{4}server=").unwrap();
|
|
let caps = re.captures(&s).unwrap();
|
|
return caps.get(1).map_or("", |m| m.as_str()).to_owned();
|
|
},
|
|
Err(_e) => "".to_owned()
|
|
}
|
|
}
|
|
|
|
pub fn get_radio_source(&self) -> rodio::Decoder<std::io::BufReader<std::fs::File>> {
|
|
Self::wait_for_source();
|
|
let file: std::io::BufReader<std::fs::File> = std::io::BufReader::new(std::fs::File::open(&crate::RADIO_STREAM).unwrap());
|
|
let source: rodio::Decoder<std::io::BufReader<std::fs::File>> = rodio::Decoder::new(file).unwrap();
|
|
return source
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|