From ad4a4e2923319ad52a1f335f71049da8922adc85 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Wed, 1 Feb 2023 21:58:14 +0100 Subject: [PATCH] ignore files that can't be decoded as audio --- src/main.rs | 17 +++++++++-------- src/sound.rs | 23 ++++++++++++----------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index c6fd059..2064b30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,14 +84,15 @@ impl App { let mut snoud = Snoud::new(); for file in files { - let internal_volume = snoud.add_channel(&file.path()); - let ui_channel = UIChannel { - name: file.file_name().to_string_lossy().into(), - volume: 100, - volume_sync: internal_volume, - muted: false, - }; - self.channels.push(ui_channel); + if let Some(internal_volume) = snoud.add_channel(&file.path()) { + let ui_channel = UIChannel { + name: file.file_name().to_string_lossy().into(), + volume: 100, + volume_sync: internal_volume, + muted: false, + }; + self.channels.push(ui_channel); + } } self.sink.append(snoud); diff --git a/src/sound.rs b/src/sound.rs index ca7eba1..a2bbc36 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -20,17 +20,15 @@ struct SoundChannel { } impl SoundChannel { - fn new(name: &PathBuf) -> Self { + fn new(name: &PathBuf) -> Option { let file = File::open(name).expect("File not found"); - let source = Decoder::new(file) - .expect("Could not decode file") - .repeat_infinite(); - Self { + let source = Decoder::new(file).ok()?.repeat_infinite(); + Some(Self { source, paused: false, volume: 1.0, volume_sync: Arc::new(Mutex::new(1.0)), - } + }) } } @@ -82,11 +80,14 @@ impl Snoud { } } - pub fn add_channel(&mut self, filename: &PathBuf) -> Arc> { - let new = SoundChannel::new(filename); - let volume_sync = new.volume_sync.clone(); - self.channels.push(new); - volume_sync + pub fn add_channel(&mut self, filename: &PathBuf) -> Option>> { + if let Some(new) = SoundChannel::new(filename) { + let volume_sync = new.volume_sync.clone(); + self.channels.push(new); + Some(volume_sync) + } else { + None + } } fn sync(&mut self) {