From 8658fabc8e0a950f2ffdc588feb2aaec0c4cc8f3 Mon Sep 17 00:00:00 2001 From: gallant Date: Sat, 1 Oct 2022 14:15:43 -0500 Subject: [PATCH] added music theory --- Cargo.toml | 3 +++ src/lib.rs | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d462e16..f77b761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,9 @@ exclude = ["src/main.rs"] version = "0.35.2" features = ["image", "ttf"] +[dependencies] +rodio = "0.15.0" + [features] default = [] static = ["sdl2/static-link", "sdl2/bundled"] diff --git a/src/lib.rs b/src/lib.rs index a34cec4..c6ca30c 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 sdl2::{ image::ImageRWops, mouse::MouseButton, @@ -112,7 +112,9 @@ use sdl2::{ video::{Window, WindowBuildError, WindowContext}, EventPump, IntegerOrSdlError, }; - +use std::fs::File; +use std::io::BufReader; +use rodio::{Decoder, OutputStream, source::Source}; use vec2::Vec2Int; #[doc(no_inline)] @@ -758,7 +760,22 @@ impl Game { Ok(()) } + pub fn play(x: String, y: u64) { + 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()); + // 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)); + + }); + } /// Stops the game loop. This method should be called inside the closure that you passed to [`Self::run()`]. /// ``` /// # use cat_box::Game;