-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow re-use of an existing audio sink. #12035
base: main
Are you sure you want to change the base?
Conversation
make AudioSink::sink public
Make SpatialAudioSink::sink public
@@ -85,7 +85,7 @@ pub trait AudioSinkPlayback { | |||
/// that source is unchanged, that translates to the audio restarting. | |||
#[derive(Component)] | |||
pub struct AudioSink { | |||
pub(crate) sink: Sink, | |||
pub sink: Sink, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc strings for these fields now that they're pub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya sorry this should be draft still
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you demonstrate how to take advantage of this for users? An example would be ideal, but a doc test would work too.
There is some work that would need to be done, for my use it's a bit messy yet. I'm basically reproducing a subset of the normal playback done in bevy_audio itself, so I'd like to bring support for what I'm doing into bevy_audio. pub(crate) fn unit_audio(
mut commands: Commands,
global_volume: Res<GlobalVolume>,
audio_sources: Res<Assets<AudioSource>>,
tracks: Res<AudioAssets>,
mut unit_q: Query<
(
Entity,
&mut AudioActions,
Option<&Handle<AudioSource>>,
Option<&mut AudioSink>,
Option<&PlaybackSettings>,
),
(With<Unit>, Changed<AudioActions>),
>,
) {
for (entity, mut audio_actions, source, mut sink, settings) in &mut unit_q {
for action in audio_actions.iter() {
if let Some(_) = source {
let source = tracks.foreground_tracks[action.as_str()].clone_weak();
let Some(audio_source) = audio_sources.get(source) else {
continue;
};
{
let sink = sink.as_mut().unwrap();
let settings = settings.as_ref().unwrap();
sink.set_speed(settings.speed);
sink.set_volume(settings.volume.get() * global_volume.volume.get());
if settings.paused {
sink.pause();
}
}
sink.as_ref().unwrap().sink.append(audio_source.decoder());
} else {
commands.entity(entity).insert(AudioBundle {
source: tracks.foreground_tracks[action.as_str()].clone_weak(),
settings: PlaybackSettings::ONCE,
});
}
}
audio_actions.clear();
}
} |
That's a pretty rough interface :/ maybe a very simple example to get people started |
The above was just to show my current use-case to Alice, planning on adding support directly into bevy_audio once there has been a discussion about it. That example will not be included for any public consumption, more of a POC. |
Objective
Some users may benefit from avoiding archetype moves on entities and re-use of a sink on an entity, this also has some side benefits such as fine-grained control and inhibition of playing a current track if the current track is deemed to be a higher priority.
PlaybackSettings::ONCE
policy,Solution
Changelog
Migration Guide