From 7a2e9a082cb1f6c2402d72b1020b8db3f290dbc9 Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Thu, 9 Mar 2023 02:52:14 +0000
Subject: [PATCH] Add convenience methods for working directly with handles

---
 src/music_box.rs | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/src/music_box.rs b/src/music_box.rs
index ec023cd..3c27cf4 100644
--- a/src/music_box.rs
+++ b/src/music_box.rs
@@ -352,6 +352,43 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
 		self.settings.ui_volume = level;
 	}
 
+	/// Stop an audio instance handle from playing immediately
+	pub fn stop_handle(&mut self, handle: &Handle<AudioInstance>) {
+		if let Some(instance) = self.audio_instances.get_mut(handle) {
+			instance.stop(AudioTween::default());
+		}
+	}
+	/// Stop an audio instance handle from playing with a given tween
+	pub fn stop_handle_with_tween(&mut self, handle: &Handle<AudioInstance>, tween: AudioTween) {
+		if let Some(instance) = self.audio_instances.get_mut(handle) {
+			instance.stop(tween);
+		}
+	}
+	/// Pause an audio instance handle immediately, in a way that can be resumed later
+	pub fn pause_handle(&mut self, handle: &Handle<AudioInstance>) {
+		if let Some(instance) = self.audio_instances.get_mut(handle) {
+			instance.pause(AudioTween::default());
+		}
+	}
+	/// Pause an audio instance with a given tween in a way that can be resumed later
+	pub fn pause_handle_with_tween(&mut self, handle: &Handle<AudioInstance>, tween: AudioTween) {
+		if let Some(instance) = self.audio_instances.get_mut(handle) {
+			instance.pause(tween);
+		}
+	}
+	/// Stop an audio instance handle from playing immediately
+	pub fn resume_handle(&mut self, handle: &Handle<AudioInstance>) {
+		if let Some(instance) = self.audio_instances.get_mut(handle) {
+			instance.resume(AudioTween::default());
+		}
+	}
+	/// Stop an audio instance handle from playing with a given tween
+	pub fn resume_handle_with_tween(&mut self, handle: &Handle<AudioInstance>, tween: AudioTween) {
+		if let Some(instance) = self.audio_instances.get_mut(handle) {
+			instance.resume(tween);
+		}
+	}
+
 	fn map_tracks<Name: ToString>(&'w self, name: Name) -> TrackType<Handle<AudioSource>> {
 		match self.handles.resolve_track_name(name) {
 			TrackType::Single(name) => match self.handles.get_audio_track(name) {
-- 
GitLab