Skip to content

Add AudioSinkPlayback::position #19173

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/bevy_audio/src/sinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ pub trait AudioSinkPlayback {
/// No effect if not paused.
fn play(&self);

/// Returns the position of the sound that's being played.
///
/// This takes into account any speedup or delay applied.
///
/// Example: if you [`set_speed(2.0)`](Self::set_speed) and [`position()`](Self::position) returns *5s*,
/// then the position in the recording is *10s* from its start.
fn position(&self) -> Duration;

/// Attempts to seek to a given position in the current source.
///
/// This blocks between 0 and ~5 milliseconds.
Expand Down Expand Up @@ -181,6 +189,10 @@ impl AudioSinkPlayback for AudioSink {
self.sink.play();
}

fn position(&self) -> Duration {
self.sink.get_pos()
}

fn try_seek(&self, pos: Duration) -> Result<(), SeekError> {
self.sink.try_seek(pos)
}
Expand Down Expand Up @@ -281,6 +293,10 @@ impl AudioSinkPlayback for SpatialAudioSink {
self.sink.play();
}

fn position(&self) -> Duration {
self.sink.get_pos()
}

fn try_seek(&self, pos: Duration) -> Result<(), SeekError> {
self.sink.try_seek(pos)
}
Expand Down
29 changes: 28 additions & 1 deletion examples/audio/audio_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (update_speed, pause, mute, volume))
.add_systems(
Update,
(update_progress_text, update_speed, pause, mute, volume),
)
.run();
}

Expand All @@ -16,6 +19,17 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
MyMusic,
));

commands.spawn((
Text::new(""),
Node {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
ProgressText,
));

// example instructions
commands.spawn((
Text::new("-/=: Volume Down/Up\nSpace: Toggle Playback\nM: Toggle Mute"),
Expand All @@ -34,10 +48,23 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
#[derive(Component)]
struct MyMusic;

#[derive(Component)]
struct ProgressText;

fn update_progress_text(
music_controller: Single<&AudioSink, With<MyMusic>>,
mut progress_text: Single<&mut Text, With<ProgressText>>,
) {
progress_text.0 = format!("Progress: {}s", music_controller.position().as_secs_f32());
}

fn update_speed(music_controller: Query<&AudioSink, With<MyMusic>>, time: Res<Time>) {
let Ok(sink) = music_controller.single() else {
return;
};
if sink.is_paused() {
return;
}

sink.set_speed((ops::sin(time.elapsed_secs() / 5.0) + 1.0).max(0.1));
}
Expand Down