Android Open Source - game-api-android Game Sound






From Project

Back to project page game-api-android.

License

The source code is released under:

MIT License

If you think the Android project game-api-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.gameengine.icadroids.sound;
//from  w w  w.j a  v  a 2 s.  com
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.gameengine.icadroids.engine.GameEngine;
import android.media.AudioManager;
import android.media.SoundPool;

/**
 * This class features a number of static methods that can be used to add sound
 * to your game.
 * 
 * This class should be used when you want to play short sound clips, the length
 * of these clips should not be longer than about 5 seconds. For larger files,
 * use the MusicPlayer class.
 * 
 * @author Leon & Lex
 * 
 */
public final class GameSound {

  private static SoundPool soundPool;
  private static Context context;
  private static HashMap<Integer, Integer> soundPoolMap;
  private static HashMap<Integer, Integer> currentSoundMap;
  private static AudioManager audioManager;
  private static boolean hasPlayed = false;
  private static int maxStreams = 20;
  private static ArrayList<Integer> soundList;

  /**
   * Constructs an instance of this class.
   */
  public GameSound() {
    initSounds(GameEngine.getAppContext());
  }

  /**
   * Initializes the storage for the sounds.
   * 
   * @param theContext
   *            The Application context.
   */
  public static final void initSounds(Context theContext) {
    context = theContext;
    soundList = new ArrayList<Integer>();
    soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
    soundPoolMap = new HashMap<Integer, Integer>();
    currentSoundMap = new HashMap<Integer, Integer>();
    audioManager = (AudioManager) context
        .getSystemService(Context.AUDIO_SERVICE);
  }

  /**
   * Adds a new sound to the SoundPool. Call this method to store a sound, you
   * can then retrieve the sound from the SoundPool with the playSound
   * function.
   * 
   * @param index
   *            The sound index where the sound will be stored.
   * @param soundID
   *            The name of the sound that needs to be stored.
   */
  public static final void addSound(int index, String soundID) {
    int resID = GameEngine
        .getAppContext()
        .getResources()
        .getIdentifier(soundID, "raw",
            GameEngine.getAppContext().getPackageName());
    soundPoolMap.put(index, soundPool.load(context, resID, 1));
  }

  /**
   * Plays a sound that has been added to the SoundPool with addSound
   * 
   * @param index
   *            The Index of the Sound to be played, defined by addSound.
   * @param loopSound
   *            The amount of times the music should loop, a negative value
   *            will result in an endless loop.
   */
  public static final void playSound(int index, int loopSound) {
    float streamVolume = audioManager
        .getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume
        / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int i = soundPool.play(soundPoolMap.get(index), streamVolume,
        streamVolume, 1, loopSound, 1f);
    currentSoundMap.put(index, i);
    soundList.add(i);
    hasPlayed = true;
  }

  /**
   * Pause a sound, call resumeSound to continue playing the specified sound.
   * 
   * @param index
   *            index of the sound to be paused.
   */
  public static final void pauseSound(int index) {
    if (currentSoundMap.get(index) != null) {
      soundPool.pause(currentSoundMap.get(index));
    }
  }

  /**
   * Pauses all sounds, call resumeSounds to continue playing sounds.
   */
  public static final void pauseSounds() {
    soundPool.autoPause();
  }

  /**
   * Resumes the specified paused sound.
   * 
   * @param index
   *            index of the sound to be resumed
   */
  public static final void resumeSound(int index) {
    if (currentSoundMap.get(index) != null) {
      soundPool.resume(currentSoundMap.get(index));
    }
  }

  /**
   * Resumes all paused sounds.
   */
  public static final void resumeSounds() {
    soundPool.autoResume();
  }

  /**
   * Stops a sound
   * 
   * @param index
   *            index of the sound to be stopped
   */
  public static final void stopSound(int index) {
    if (currentSoundMap.get(index) != null) {
      soundPool.stop(currentSoundMap.get(index));
    }
  }

  /**
   * Stops all sounds.
   */
  public static final void stopSounds() {
    if (soundList != null) {
      for (int i = 0; i < soundList.size(); i++) {
        soundPool.stop(soundList.get(i));
      }
    }
  }

  /**
   * Deallocates the resources and Instance of GameSound.This is called
   * automatically when the application is be closed.
   */
  public static final void cleanup() {
    if (hasPlayed) {
      soundPool.release();
      soundPool = null;
      soundPoolMap.clear();
      audioManager.unloadSoundEffects();
    }
  }
}




Java Source Code List

android.gameengine.icadroids.alarms.Alarm.java
android.gameengine.icadroids.alarms.IAlarm.java
android.gameengine.icadroids.dashboard.DashboardImageView.java
android.gameengine.icadroids.dashboard.DashboardTextView.java
android.gameengine.icadroids.engine.GameEngine.java
android.gameengine.icadroids.engine.GameFPSCounter.java
android.gameengine.icadroids.engine.GameThread.java
android.gameengine.icadroids.engine.GameView.java
android.gameengine.icadroids.engine.Viewport.java
android.gameengine.icadroids.forms.GameForm.java
android.gameengine.icadroids.forms.IFormInput.java
android.gameengine.icadroids.forms.ViewCreator.java
android.gameengine.icadroids.forms.ViewRemover.java
android.gameengine.icadroids.input.MotionSensor.java
android.gameengine.icadroids.input.OnScreenButton.java
android.gameengine.icadroids.input.OnScreenButtons.java
android.gameengine.icadroids.input.TouchInput.java
android.gameengine.icadroids.objects.GameObject.java
android.gameengine.icadroids.objects.MoveableGameObject.java
android.gameengine.icadroids.objects.collisions.CollidingObject.java
android.gameengine.icadroids.objects.collisions.ICollision.java
android.gameengine.icadroids.objects.collisions.TileCollision.java
android.gameengine.icadroids.objects.graphics.AnimatedSprite.java
android.gameengine.icadroids.objects.graphics.Sprite.java
android.gameengine.icadroids.persistence.GamePersistence.java
android.gameengine.icadroids.sound.GameSound.java
android.gameengine.icadroids.sound.MusicPlayer.java
android.gameengine.icadroids.tiles.GameTiles.java
android.gameengine.icadroids.tiles.Tile.java
com.android.vissenspel.Monster.java
com.android.vissenspel.StrawberryControler.java
com.android.vissenspel.Strawberry.java
com.android.vissenspel.Vis.java
com.android.vissenspel.Vissenkom.java
game.MyAndroidGame.java
testGames.AndroidCraft_demo.java
testGames.FormTest.java
testGames.Player.java
testGames.TestGameBas.java
testGames.TestGameLex.java
testGames.TestGameRoel.java
testGames.gameEngineTest.DebugEngine.java
testGames.gameEngineTest.GameEngineTestGame.java
testGames.gameEngineTest.TestGameObject.java
testGames.gameEngineTest.debugObject.java
testGames.gameEngineTest.randomObject.java
testGames.testGameObjectBas.java
testGames.testGame.java