Dog Audio - UQcsse3200/2024-studio-2 GitHub Wiki

DogSoundPlayer

Overview

The DogSoundPlayer class is responsible for managing the playback of dog-related sounds. This class focuses on handling the panting and other dog-specific sounds such as barking, using LibGDX's Sound interface.

Purpose

The DogSoundPlayer is responsible for:

  • Playing dog-specific sounds such as panting and barking.
  • Managing the looped playback of panting sound during movement.
  • Providing methods to control the volume and stop sounds as needed.

Structure

Constructor

The constructor initializes the DogSoundPlayer by passing in two Sound objects for panting and barking. These sounds are used to manage playback during gameplay.

public DogSoundPlayer(Sound pantingSound, Sound barkingSound) {
    this.pantingSound = pantingSound;
    this.barkingSound = barkingSound;
}

Methods

playPantingSound()

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

public void playPantingSound() {
    if (pantingSoundId == -1) {
        Sound pantingSound = ServiceLocator.getResourceService().getAsset(pantingSoundPath, Sound.class);
        if (pantingSound != null) {
            float volume = AudioManager.getSoundVolume();
            pantingSoundId = pantingSound.loop(volume);
            logger.info("Panting sound started looping with volume: {}", volume);
        }
    }
}

stopPantingSound()

Stops the panting sound if it is currently playing.

public void stopPantingSound() {
    if (pantingSoundId != -1) {
        pantingSound.stop(pantingSoundId);
        pantingSoundId = -1;
        logger.info("Panting sound stopped.");
    }
}

updatePantingSound(boolean isMoving, float volume)

Updates the panting sound's state based on whether the dog is moving. Plays the panting sound if the dog is moving, otherwise stops it.

public void updatePantingSound(boolean isMoving, float volume) {
    if (isMoving) {
        playPantingSound(volume);
    } else {
        stopPantingSound();
    }
}

playBarkingSound(float volume)

Plays the barking sound once, using the volume from the AudioManager.

public void playBarkingSound() {
    Sound barkingSound = ServiceLocator.getResourceService().getAsset(barkingSoundPath, Sound.class);
    if (barkingSound != null) {
        float volume = AudioManager.getSoundVolume();
        barkingSound.play(volume);
        logger.info("Barking sound played with volume: {}", volume);
    }
}

Usage

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

DogSoundPlayer dogSoundPlayer = new DogSoundPlayer("dog_panting.mp3", "dog_barking.mp3");

dogSoundPlayer.updatePantingSound(isDogMoving);
dogSoundPlayer.playBarkingSound();

Testing

UML

image

Back