Water Animal Audio - UQcsse3200/2024-studio-2 GitHub Wiki
WaterAnimalSoundPlayer
Overview
The WaterAnimalSoundPlayer class is responsible for managing the playback of water-animal-related sounds, such as swimming. It leverages the ServiceLocator to load sounds and the AudioManager to control their volume.
Purpose
The WaterAnimalSoundPlayer
is designed to:
- Play swimming sounds.
- Manage the looped playback of the swimming sound when the animal is in motion.
- Offer methods to control the volume and stop sounds as necessary.
Structure
Constructor
The constructor initializes the WaterAnimalSoundPlayer
by accepting a Sound
object for swimming. This sound is utilized to manage sound playback during the game.
public WaterAnimalSoundPlayer(Sound swimmingSound) {
this.swimmingSound = swimmingSound;
}
Methods
playSwimmingSound()
Plays the swimming sound in a loop if it is not already playing, using the volume from the AudioManager.
public void playSwimmingSound() {
if (swimmingSoundId == -1) {
Sound swimmingSound = ServiceLocator.getResourceService().getAsset(swimmingSoundPath, Sound.class);
if (swimmingSound != null) {
float volume = AudioManager.getSoundVolume();
swimmingSoundId = swimmingSound.loop(volume);
logger.info("Swimming sound started looping with volume: {}", volume);
}
}
}
stopSwimmingSound()
Stops the swimming sound if it is currently playing.
public void stopSwimmingSound() {
if (swimmingSoundId != -1) {
swimmingSound.stop(swimmingSoundId);
swimmingSoundId = -1;
logger.info("Swimming sound stopped.");
}
}
updateSwimmingSound(boolean isSwimming, float volume)
Updates the state of the swimming sound based on whether the animal is swimming. It starts the swimming sound if the animal is swimming and stops it when the animal is stationary.
public void updateSwimmingSound(boolean isSwimming) {
if (isSwimming) {
playSwimmingSound();
} else {
stopSwimmingSound();
}
}
Usage
To use the WaterAnimalSoundPlayer, instantiate it with a file path for the swimming sound, then control the playback of this sound based on the game’s logic.
WaterAnimalSoundPlayer waterAnimalSoundPlayer = new WaterAnimalSoundPlayer("water_swimming.mp3");
waterAnimalSoundPlayer.updateSwimmingSound(isFishSwimming);