AudioPlayer::new() (#16287)

# Objective

`AudioPlayer::<AudioSource>(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.
This commit is contained in:
Carter Anderson 2024-11-07 17:51:50 -08:00 committed by GitHub
parent 2b434035b7
commit 013e11a14f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 17 additions and 6 deletions

View file

@ -264,6 +264,17 @@ where
}
}
impl AudioPlayer<AudioSource> {
/// Creates a new [`AudioPlayer`] with the given [`Handle<AudioSource>`].
///
/// 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<AudioSource>) -> Self {
Self(source)
}
}
/// Bundle for playing a sound.
///
/// Insert this bundle onto an entity to trigger a sound source to begin playing.

View file

@ -21,7 +21,7 @@
//!
//! fn play_background_audio(asset_server: Res<AssetServer>, mut commands: Commands) {
//! commands.spawn((
//! AudioPlayer::<AudioSource>(asset_server.load("background_audio.ogg")),
//! AudioPlayer::new(asset_server.load("background_audio.ogg")),
//! PlaybackSettings::LOOP,
//! ));
//! }

View file

@ -11,7 +11,7 @@ fn main() {
}
fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn(AudioPlayer::<AudioSource>(
commands.spawn(AudioPlayer::new(
asset_server.load("sounds/Windless Slopes.ogg"),
));
}

View file

@ -12,7 +12,7 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
AudioPlayer::<AudioSource>(asset_server.load("sounds/Windless Slopes.ogg")),
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
MyMusic,
));
}

View file

@ -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::<AudioSource>(asset_server.load("sounds/Windless Slopes.ogg")),
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
PlaybackSettings::LOOP.with_spatial(true),
));

View file

@ -29,7 +29,7 @@ fn setup(
MeshMaterial3d(materials.add(Color::from(BLUE))),
Transform::from_xyz(0.0, 0.0, 0.0),
Emitter::default(),
AudioPlayer::<AudioSource>(asset_server.load("sounds/Windless Slopes.ogg")),
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
PlaybackSettings::LOOP.with_spatial(true),
));

View file

@ -162,7 +162,7 @@ fn button_handler(
fn setup_music(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn((
AudioPlayer::<AudioSource>(asset_server.load("sounds/Windless Slopes.ogg")),
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
PlaybackSettings::LOOP,
));
}