From 013e11a14f3554cbeaeb157557dca6ee473fcce1 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 7 Nov 2024 17:51:50 -0800 Subject: [PATCH] AudioPlayer::new() (#16287) # Objective `AudioPlayer::(assets.load("audio.mp3"))` is awkward and complicated to type because the `AudioSource` generic type cannot be elided. This is especially annoying because `AudioSource` is used in the majority of cases. Most users don't need to think about it. ## Solution Add an `AudioPlayer::new()` function that is hard-coded to `AudioSource`, allowing `AudioPlayer::new(assets.load("audio.mp3"))`. Prefer using that in the relevant places. --- crates/bevy_audio/src/audio.rs | 11 +++++++++++ crates/bevy_audio/src/lib.rs | 2 +- examples/audio/audio.rs | 2 +- examples/audio/audio_control.rs | 2 +- examples/audio/spatial_audio_2d.rs | 2 +- examples/audio/spatial_audio_3d.rs | 2 +- examples/mobile/src/lib.rs | 2 +- 7 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/bevy_audio/src/audio.rs b/crates/bevy_audio/src/audio.rs index 58dc0e49a1..7c020a0333 100644 --- a/crates/bevy_audio/src/audio.rs +++ b/crates/bevy_audio/src/audio.rs @@ -264,6 +264,17 @@ where } } +impl AudioPlayer { + /// Creates a new [`AudioPlayer`] with the given [`Handle`]. + /// + /// For convenience reasons, this hard-codes the [`AudioSource`] type. If you want to + /// initialize an [`AudioPlayer`] with a different type, just initialize it directly using normal + /// tuple struct syntax. + pub fn new(source: Handle) -> Self { + Self(source) + } +} + /// Bundle for playing a sound. /// /// Insert this bundle onto an entity to trigger a sound source to begin playing. diff --git a/crates/bevy_audio/src/lib.rs b/crates/bevy_audio/src/lib.rs index 5120a341a1..1519987a4b 100644 --- a/crates/bevy_audio/src/lib.rs +++ b/crates/bevy_audio/src/lib.rs @@ -21,7 +21,7 @@ //! //! fn play_background_audio(asset_server: Res, mut commands: Commands) { //! commands.spawn(( -//! AudioPlayer::(asset_server.load("background_audio.ogg")), +//! AudioPlayer::new(asset_server.load("background_audio.ogg")), //! PlaybackSettings::LOOP, //! )); //! } diff --git a/examples/audio/audio.rs b/examples/audio/audio.rs index 5f4225fc73..7ca6abcdec 100644 --- a/examples/audio/audio.rs +++ b/examples/audio/audio.rs @@ -11,7 +11,7 @@ fn main() { } fn setup(asset_server: Res, mut commands: Commands) { - commands.spawn(AudioPlayer::( + commands.spawn(AudioPlayer::new( asset_server.load("sounds/Windless Slopes.ogg"), )); } diff --git a/examples/audio/audio_control.rs b/examples/audio/audio_control.rs index f81f7ea89b..b56865c891 100644 --- a/examples/audio/audio_control.rs +++ b/examples/audio/audio_control.rs @@ -12,7 +12,7 @@ fn main() { fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(( - AudioPlayer::(asset_server.load("sounds/Windless Slopes.ogg")), + AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")), MyMusic, )); } diff --git a/examples/audio/spatial_audio_2d.rs b/examples/audio/spatial_audio_2d.rs index 887a4d70cd..f0cedc9b9b 100644 --- a/examples/audio/spatial_audio_2d.rs +++ b/examples/audio/spatial_audio_2d.rs @@ -38,7 +38,7 @@ fn setup( MeshMaterial2d(materials.add(Color::from(BLUE))), Transform::from_translation(Vec3::new(0.0, 50.0, 0.0)), Emitter::default(), - AudioPlayer::(asset_server.load("sounds/Windless Slopes.ogg")), + AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")), PlaybackSettings::LOOP.with_spatial(true), )); diff --git a/examples/audio/spatial_audio_3d.rs b/examples/audio/spatial_audio_3d.rs index 6695a01bcf..5ea1b6035c 100644 --- a/examples/audio/spatial_audio_3d.rs +++ b/examples/audio/spatial_audio_3d.rs @@ -29,7 +29,7 @@ fn setup( MeshMaterial3d(materials.add(Color::from(BLUE))), Transform::from_xyz(0.0, 0.0, 0.0), Emitter::default(), - AudioPlayer::(asset_server.load("sounds/Windless Slopes.ogg")), + AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")), PlaybackSettings::LOOP.with_spatial(true), )); diff --git a/examples/mobile/src/lib.rs b/examples/mobile/src/lib.rs index 978c291288..bf4e411811 100644 --- a/examples/mobile/src/lib.rs +++ b/examples/mobile/src/lib.rs @@ -162,7 +162,7 @@ fn button_handler( fn setup_music(asset_server: Res, mut commands: Commands) { commands.spawn(( - AudioPlayer::(asset_server.load("sounds/Windless Slopes.ogg")), + AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")), PlaybackSettings::LOOP, )); }