Skip to content

Commit

Permalink
Fail silently on sound error
Browse files Browse the repository at this point in the history
  • Loading branch information
vjousse committed Jun 2, 2024
1 parent 614ec2c commit 0fe4df6
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use image::{ImageBuffer, Rgba};
use rodio::{Decoder, OutputStream, Sink};
use serde::ser::Error;
use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;
Expand Down Expand Up @@ -235,7 +236,8 @@ async fn tick(app_handle: AppHandle, path: String) {

tauri::async_runtime::spawn_blocking(move || {
if play_tick {
play_sound_file(&new_path);
// Fail silently if we can't play sound file
let _ = play_sound_file(&new_path);
}
});
}
Expand Down Expand Up @@ -459,18 +461,19 @@ fn get_sound_file(sound_id: &str) -> Option<&str> {
}
}

fn play_sound_file(resource_path: &str) {
fn play_sound_file(resource_path: &str) -> Result<(), Box<dyn std::error::Error>> {
// Get a output stream handle to the default physical sound device
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
let (_stream, stream_handle) = OutputStream::try_default()?;
let sink = Sink::try_new(&stream_handle)?;

// Load a sound from a file, using a path relative to Cargo.toml
let file = BufReader::new(File::open(resource_path).unwrap());
let file = BufReader::new(File::open(resource_path)?);

// Decode that sound file into a source
let source = Decoder::new(file).unwrap();
let source = Decoder::new(file)?;
sink.append(source);
sink.sleep_until_end();
Ok(())
}

#[tauri::command]
Expand All @@ -483,7 +486,8 @@ async fn play_sound_command(app_handle: tauri::AppHandle, sound_id: String) {
.unwrap();
let path = resource_path.to_string_lossy();

play_sound_file(&path);
// Fail silently if we can't play sound file
let _ = play_sound_file(&path);
}

#[tauri::command]
Expand Down

0 comments on commit 0fe4df6

Please sign in to comment.