diff --git a/Cargo.toml b/Cargo.toml index f77b761..a9a9d00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,9 @@ version = "0.35.2" features = ["image", "ttf"] [dependencies] -rodio = "0.15.0" +rodio = { version = "0.11.1", optional = true} [features] default = [] static = ["sdl2/static-link", "sdl2/bundled"] +audio = ["dep:rodio"] diff --git a/output.mp3 b/output.mp3 new file mode 100644 index 0000000..99ba4f1 Binary files /dev/null and b/output.mp3 differ diff --git a/src/lib.rs b/src/lib.rs index 3735ef5..d5a84db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,7 +100,7 @@ use std::{ path::Path, slice::IterMut, }; -use std::{thread, time, process::Command}; +use std::{thread}; use sdl2::{ image::ImageRWops, mouse::MouseButton, @@ -776,20 +776,19 @@ impl Game { /// ``` /// play(String::from("/path/to/song.mp3", 15)); /// ``` -pub fn play(x: String, y: u64) -> thread::JoinHandle<()> { - let wack = thread::spawn(move || { +#[cfg(feature = "audio")] +pub fn play + std::marker::Send + 'static>(x: P, y: u64) -> thread::JoinHandle<()> { + thread::spawn(move || { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); // Load a sound from a file, using a path relative to Cargo.toml let file = BufReader::new(File::open(x).unwrap()); // Decode that sound file into a source let source = Decoder::new(file).unwrap(); // Play the sound directly on the device - stream_handle.play_raw(source.convert_samples()); + stream_handle.play_raw(source.convert_samples()).unwrap(); // The sound plays in a separate audio thread, // so we need to keep the main thread alive while it's playing. std::thread::sleep(std::time::Duration::from_secs(y)); - - }); - wack + }) } diff --git a/src/main.rs b/src/main.rs index 77599ea..2c2b047 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ #![warn(clippy::pedantic)] -use cat_box::{draw_text, get_keyboard_state, get_mouse_state, Game, Sprite, SpriteCollection}; +use cat_box::{draw_text, get_keyboard_state, get_mouse_state, Game, Sprite, SpriteCollection, play}; use sdl2::keyboard::Scancode; fn main() { @@ -19,6 +19,7 @@ fn main() { coll.push(x); } } + play(String::from("output.mp3"), 120); game.run(|ctx| { i = (i + 1) % 255; ctx.set_background_colour(i as u8, 64, 255);