Android Open Source - LucyTheMoocher Sound Manager






From Project

Back to project page LucyTheMoocher.

License

The source code is released under:

MIT License

If you think the Android project LucyTheMoocher 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 com.lucythemoocher.sounds;
//from  w  w  w.  j a  v a2 s  . c o  m
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import com.lucythemoocher.R;
import com.lucythemoocher.Globals.Globals;
import com.lucythemoocher.util.Resources;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

/**
 * Class aimed to play sounds\n
 * Warning : I use SoundPool to play the music, this is bad because SoundPool
 * uses audio stream uncompressed in memory, but this is the only way to have the power
 * to choice the speed of the sounds.\n
 * N.B. the music samples must have the exact same duration !
 */
public class SoundManager {

  private static final int BACKGROUND_THEME = 0;
  private static final int BACKGROUND_LVL1 = 1;
  private static final int BACKGROUND_LVL2 = 2;
  private static final int BACKGROUND_LVL3 = 3;

  private SoundPool sounds_ = null;
  private HashMap<Integer, Integer> loaded_ = null;
  private HashMap<Integer, Integer> playing_ = null;
  private Vector<ArrayList<Integer>> soundsBackground_;
  private int sampleDuration_;
  private float lastPlay_;
  private boolean started_;
  private SoundsState state_;
  private MediaPlayer player_;
  
  public SoundManager() {
    player_ = MediaPlayer.create(Resources.getActivity(), R.raw.theme_loop);
  }

  /**
   * Update
   */
  public void update() {}
  
  /**
   * Load sound resources
   */
  public void load() {}

  /**
   * Start playing
   */
  public void start() {
    player_.start();
    player_.setLooping(true);
  }
  
  /**
   * Stop playing and rewind
   */
  public void stop() {
    player_.pause();
    player_.seekTo(0);
  }
  
  /**
   * Pause
   */
  public void pause() {
    player_.pause();
  }
  
  /**
   * Resume
   */
  public void resume() {
    player_.start();
  }

  /**
   * Play sound from resource id
   * @param resource
   */
  void playSound(int resource) {
    AudioManager mgr = (AudioManager)Resources.getActivity().getSystemService(Context.AUDIO_SERVICE);
    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    float volume = streamVolumeCurrent / streamVolumeMax;
    //TODO last argument should be the game speed
    playing_.put(resource, sounds_.play(loaded_.get(resource), volume, volume, 1, 0, 1f));
  }
  
  /**
   * Change state
   * @param newState
   */
  void changeState(SoundsState newState) {
    state_ = newState;
  }
}




Java Source Code List

com.lucythemoocher.LucyTheMoocherActivity.java
com.lucythemoocher.FX.FXManager.java
com.lucythemoocher.FX.FX.java
com.lucythemoocher.Globals.Globals.java
com.lucythemoocher.actors.Actor.java
com.lucythemoocher.actors.ActorsManager.java
com.lucythemoocher.actors.Monster.java
com.lucythemoocher.actors.MonstersManager.java
com.lucythemoocher.actors.PlayerCharacter.java
com.lucythemoocher.actors.Projectile.java
com.lucythemoocher.actors.ProjectilesManager.java
com.lucythemoocher.actors.Tank.java
com.lucythemoocher.actors.TargetCharacter.java
com.lucythemoocher.actors.maincharacter.state.StateAttack.java
com.lucythemoocher.actors.maincharacter.state.StateFalling.java
com.lucythemoocher.actors.maincharacter.state.StateJumping.java
com.lucythemoocher.actors.maincharacter.state.StateNone.java
com.lucythemoocher.actors.maincharacter.state.StateRunning.java
com.lucythemoocher.actors.maincharacter.state.StateWallSliding.java
com.lucythemoocher.actors.maincharacter.state.StateWallWalking.java
com.lucythemoocher.actors.maincharacter.state.State.java
com.lucythemoocher.controls.AIController.java
com.lucythemoocher.controls.ActionController.java
com.lucythemoocher.controls.ButtonListener.java
com.lucythemoocher.controls.Controllable.java
com.lucythemoocher.controls.Controller.java
com.lucythemoocher.controls.GlobalController.java
com.lucythemoocher.controls.KeysListener.java
com.lucythemoocher.controls.TouchListener.java
com.lucythemoocher.events.EventNormal.java
com.lucythemoocher.events.EventSlow.java
com.lucythemoocher.events.Event.java
com.lucythemoocher.game.GameThread.java
com.lucythemoocher.game.Game.java
com.lucythemoocher.game.LevelLoader.java
com.lucythemoocher.graphics.ActorDrawer.java
com.lucythemoocher.graphics.Animation.java
com.lucythemoocher.graphics.Background.java
com.lucythemoocher.graphics.Camera.java
com.lucythemoocher.graphics.Drawable.java
com.lucythemoocher.graphics.Grid.java
com.lucythemoocher.graphics.HUD.java
com.lucythemoocher.graphics.Image.java
com.lucythemoocher.graphics.PersistentEffect.java
com.lucythemoocher.graphics.PersistentPic.java
com.lucythemoocher.graphics.PictureContainer.java
com.lucythemoocher.gui.MenuButtonListener.java
com.lucythemoocher.gui.MenuButtonTouchListener.java
com.lucythemoocher.gui.MenuButton.java
com.lucythemoocher.loops.CreditsLoop.java
com.lucythemoocher.loops.GameOverLoop.java
com.lucythemoocher.loops.InitMenuLoop.java
com.lucythemoocher.loops.LivesMenuLoop.java
com.lucythemoocher.loops.LoopGame.java
com.lucythemoocher.loops.LoopPause.java
com.lucythemoocher.loops.Loop.java
com.lucythemoocher.loops.MasterLoop.java
com.lucythemoocher.physics.Box.java
com.lucythemoocher.physics.Cinematic.java
com.lucythemoocher.physics.Map.java
com.lucythemoocher.sounds.SoundManager.java
com.lucythemoocher.sounds.SoundsState.java
com.lucythemoocher.sounds.StateLevel1.java
com.lucythemoocher.sounds.StateLevel2.java
com.lucythemoocher.sounds.StateLevel3.java
com.lucythemoocher.sounds.StateNormal.java
com.lucythemoocher.util.Direction.java
com.lucythemoocher.util.MathUtil.java
com.lucythemoocher.util.Resources.java
com.lucythemoocher.util.Timer.java