From 9f4bfea2415502165e1075494508f7b201afd3ad Mon Sep 17 00:00:00 2001 From: Dukantic Date: Wed, 27 Aug 2025 17:30:32 +0200 Subject: [PATCH] paused current sound --- src/main.rs | 2 ++ src/music.rs | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0014ab2..7e7a09c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -266,6 +266,8 @@ impl Polymusic { self.paused = !self.paused; if !self.paused { self.time_last_frame = Instant::now(); + } else { + self.music.pause(); } } diff --git a/src/music.rs b/src/music.rs index 4f15464..0dd8226 100644 --- a/src/music.rs +++ b/src/music.rs @@ -3,6 +3,7 @@ use crate::polygon_draw::*; use crate::utils::string_to_polygon; use serde::{Deserialize, Serialize}; +use kira::sound::SoundData; use kira::{AudioManager, sound::static_sound::StaticSoundData}; use iced::event::Status; @@ -33,6 +34,9 @@ pub struct Music { pub current_delta: f32, #[serde(skip)] point_removed: Vec<(f32, PolygonFrame)>, + + #[serde(skip)] + handles_audio: Vec<::Handle>, } impl Music { @@ -80,6 +84,7 @@ impl Music { teta: 0., current_delta: 0., point_removed: vec![], + handles_audio: vec![], } } @@ -104,11 +109,22 @@ impl Music { current_frame.teta = currrent_teta; let sound_to_play = current_frame.all_sound_to_play_btw(teta_temp, current_frame.teta); + let mut handles: Vec<::Handle> = vec![]; for sound in sound_to_play { - audio_manager - .play(sound.clone()) - .expect("Error to play sound"); + handles.push( + audio_manager + .play(sound.clone()) + .expect("Error to play sound"), + ); } + self.handles_audio.append(&mut handles); + } + + pub fn pause(&mut self) { + for h in &mut self.handles_audio { + h.stop(kira::Tween::default()); + } + self.handles_audio = vec![]; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SET~~~~~~~~~~~~~~~~~~~~~~~~~~~~