From e8cd2fc7275d0799115d6db57090c2f99d9a99a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Fri, 8 Apr 2022 23:12:30 +0000 Subject: [PATCH] Audio control at start of playback (#4110) # Objective - While playing with volume, I noticed that when setting the volume just after playback start, I still get a few milliseconds at normal volume ## Solution - Replace `play_in_loop` with `play_with_settings` that allows from more controls - Adds a `PlaybackSettings` to specify the settings from start. Can be used: `PlaybackSettings::LOOP.with_volume(0.75)` --- crates/bevy_audio/src/audio.rs | 79 ++++++++++++++++++++++++--- crates/bevy_audio/src/audio_output.rs | 12 +++- crates/bevy_audio/src/lib.rs | 2 +- 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/crates/bevy_audio/src/audio.rs b/crates/bevy_audio/src/audio.rs index 4384175705..a7c3a614ba 100644 --- a/crates/bevy_audio/src/audio.rs +++ b/crates/bevy_audio/src/audio.rs @@ -78,7 +78,7 @@ where pub fn play(&self, audio_source: Handle) -> Handle { let id = HandleId::random::(); let config = AudioToPlay { - repeat: false, + settings: PlaybackSettings::ONCE, sink_handle: id, source_handle: audio_source, }; @@ -86,13 +86,31 @@ where Handle::::weak(id) } - /// Play audio from a [`Handle`] to the audio source in a loop + /// Play audio from a [`Handle`] to the audio source with [`PlaybackSettings`] that + /// allows looping or changing volume from the start. /// - /// See [`Self::play`] on how to control playback. - pub fn play_in_loop(&self, audio_source: Handle) -> Handle { + /// ``` + /// # use bevy_ecs::system::Res; + /// # use bevy_asset::AssetServer; + /// # use bevy_audio::Audio; + /// # use bevy_audio::PlaybackSettings; + /// fn play_audio_system(asset_server: Res, audio: Res