Air Animal Audio - UQcsse3200/2024-studio-2 GitHub Wiki

AirAnimalSoundPlayer

Overview

The AirAnimalSoundPlayer class is responsible for managing the playback of air-animal-related sounds, such as wings flapping and bird screeches. It leverages the ServiceLocator to load sounds and the AudioManager to control their volume.

Purpose

The AirAnimalSoundPlayer is designed to:

  • Play specific sounds like wings flapping and bird screeches.
  • Manage the looped playback of flapping sound when the animal is in motion.
  • Offer methods to control the volume and stop sounds as necessary.

Structure

Constructor

The constructor initializes the AirAnimalSoundPlayer by accepting Sound objects for flapping and screeching. These sounds are utilized to manage sound playback during the game.

public AirAnimalSoundPlayer(Sound flappingSound, Sound screechSound) {
    this.flappingSound = flappingSound;
    this.screechSound = screechSound;
}

Methods

playFlappingSound(float volume)

Plays the wings flapping sound in a loop if it is not already playing, using the volume from the AudioManager.

public void playFlappingSound() {
    if (flappingSoundId == -1) {
        Sound flappingSound = ServiceLocator.getResourceService().getAsset(flappingSoundPath, Sound.class);
        if (flappingSound != null) {
            float volume = AudioManager.getSoundVolume();
            flappingSoundId = flappingSound.loop(volume);
            logger.info("Wings flapping sound started looping with volume: {}", volume);
        }
    }
}

stopFlappingSound()

Stops the flapping sound if it is currently playing.

public void stopFlappingSound() {
    if (flappingSoundId != -1) {
        flappingSound.stop(flappingSoundId);
        flappingSoundId = -1;
        logger.info("Wings flapping sound stopped.");
    }
}

updateFlappingSound(boolean isFlying)

Updates the state of the flapping sound based on whether the animal is flying. It starts the flapping sound if the animal is flying and stops it when the animal is stationary.

public void updateFlappingSound(boolean isFlying) {
    if (isFlying) {
        playFlappingSound();
    } else {
        stopFlappingSound();
    }
}

playScreechSound()

Plays the bird screech sound once, using the volume from the AudioManager.

public void playScreechSound() {
    Sound screechSound = ServiceLocator.getResourceService().getAsset(screechSoundPath, Sound.class);
    if (screechSound != null) {
        float volume = AudioManager.getSoundVolume();
        screechSound.play(volume);
        logger.info("Bird screech sound played with volume: {}", volume);
    }
}

Usage

To use the AirAnimalSoundPlayer, instantiate it with file paths for the flapping and screeching sounds, then control the playback of these sounds based on the game’s logic.

AirAnimalSoundPlayer airAnimalSoundPlayer = new AirAnimalSoundPlayer("wings_flapping.mp3", "bird_screech.mp3");

airAnimalSoundPlayer.updateFlappingSound(isBirdFlying);
airAnimalSoundPlayer.playScreechSound();

Testing

UML

image

Back