All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lessvoid.nifty.gdx.sound.GdxMusicHandle Maven / Gradle / Ivy

The newest version!
package de.lessvoid.nifty.gdx.sound;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;

import de.lessvoid.nifty.sound.SoundSystem;
import de.lessvoid.nifty.spi.sound.SoundHandle;

import javax.annotation.Nonnull;

/**
 * This is the sound handle used for background music.
 *
 * @author Martin Karing <[email protected]>
 */
public class GdxMusicHandle implements SoundHandle {
  /**
   * The instance of the music that is played.
   */
  private final Music music;

  /**
   * The asset manager used to load the music.
   */
  @Nonnull
  private final AssetManager assetManager;

  /**
   * The volume the music is currently played at.
   */
  private float currentVolume;

  /**
   * Create a new sound handle for background music.
   *
   * @param assetManager the asset manager used to load the music
   * @param soundSystem  the sound system of the nifty gui
   * @param fileName     the file name that is the load to fetch the new music
   */
  public GdxMusicHandle(
      @Nonnull final AssetManager assetManager,
      @Nonnull final SoundSystem soundSystem,
      final String fileName) {
    this.assetManager = assetManager;

    if (!assetManager.isLoaded(fileName, Music.class)) {
      assetManager.load(fileName, Music.class);
      assetManager.finishLoading();
    }
    music = assetManager.get(fileName, Music.class);
    currentVolume = soundSystem.getMusicVolume();
    music.setVolume(currentVolume);
  }


  @Override
  public void play() {
    music.play();
  }

  @Override
  public void stop() {
    music.stop();
  }

  @Override
  public void setVolume(final float volume) {
    currentVolume = volume;
    music.setVolume(volume);
  }

  @Override
  public float getVolume() {
    return currentVolume;
  }

  @Override
  public boolean isPlaying() {
    return music.isPlaying();
  }

  @Override
  public void dispose() {
    assetManager.unload(assetManager.getAssetFileName(music));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy