audio error handling

This commit is contained in:
Max Bradbury 2020-09-23 13:43:49 +01:00
parent 1ea14e0c28
commit 6b98493e0b
1 changed files with 40 additions and 6 deletions

View File

@ -10,6 +10,29 @@ use std::process::Command;
const SPACING: i32 = 16;
fn error_popup(message: &str) {
let popup = gtk::Window::new(gtk::WindowType::Toplevel);
popup.set_title("error");
popup.set_border_width(SPACING as u32);
popup.set_position(gtk::WindowPosition::Center);
popup.set_default_size(256, 64);
let vertical = gtk::Box::new(Orientation::Vertical, SPACING);
popup.add(&vertical);
let message = gtk::Label::new(Some(message));
vertical.add(&message);
let button_ok = gtk::Button::with_label("OK");
vertical.add(&button_ok);
popup.show_all();
button_ok.connect_clicked(move |_| unsafe {
popup.destroy();
});
}
fn get_data_dir() -> PathBuf {
let mut data_dir = dirs::data_dir().expect("Couldn't find user data directory");
@ -47,10 +70,20 @@ fn build_ui(application: &gtk::Application) {
let file = File::open(&path)
.expect("Couldn't open audio file");
let source = rodio::Decoder::new(BufReader::new(file))
.expect("Couldn't decode audio");
let source = rodio::Decoder::new(
BufReader::new(file)
);
let source = source.repeat_infinite();
if source.is_err() {
error_popup(&format!(
"Couldn't parse file {}. \n{}.",
path.to_str().unwrap(),
source.err().unwrap()
));
continue;
}
let source = source.unwrap().repeat_infinite();
let sink = Sink::new(&device);
sink.append(source);
@ -97,14 +130,15 @@ fn build_ui(application: &gtk::Application) {
let row_add = gtk::Box::new(Orientation::Horizontal, SPACING);
row_add.set_homogeneous(true);
let button_open_dir = gtk::Button::with_label("manage sounds");
button_open_dir.connect_clicked(|_|{
let button_manage_sounds = gtk::Button::with_label("manage sounds");
button_manage_sounds.connect_clicked(|_| {
let mut file_manager = Command::new("xdg-open");
file_manager.arg(get_data_dir());
file_manager.output().unwrap();
});
row_add.add(&button_open_dir);
row_add.add(&button_manage_sounds);
vertical.add(&row_add);
window.show_all();