ignore files that can't be decoded as audio

This commit is contained in:
Crispy 2023-02-01 21:58:14 +01:00
parent ad5af6a27a
commit ad4a4e2923
2 changed files with 21 additions and 19 deletions

View file

@ -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);

View file

@ -20,17 +20,15 @@ struct SoundChannel {
}
impl SoundChannel {
fn new(name: &PathBuf) -> Self {
fn new(name: &PathBuf) -> Option<Self> {
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<Mutex<f32>> {
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<Arc<Mutex<f32>>> {
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) {