Android Open Source - game-api-android Music Player






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  www.  ja  va  2 s  . c  o m*/
import java.util.HashSet;
import java.io.IOException;
import android.gameengine.icadroids.engine.GameEngine;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;

/**
 * This class features a number of static methods that can be used to add music
 * to your game.
 * 
 * This class should be used when you want to play long sound clips or
 * background music. For short soundclips, use the GameSound class.
 * 
 * @author Lex & Leon
 * 
 */
public final class MusicPlayer {

  private static HashSet<MediaPlayer> mpSet = new HashSet<MediaPlayer>();

  /**
   * Plays the specified music.
   * 
   * @param resId
   *            The name of the music that needs to played.
   */
  public static void play(String resId, boolean loop) {
    int resID = GameEngine.getAppContext().getResources()
        .getIdentifier(resId, "raw",
            GameEngine.getAppContext().getPackageName());
    
    MediaPlayer mp = MediaPlayer.create(GameEngine.getAppContext(), resID);

    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    if (loop) {
      mp.setLooping(true);
    } else {
      mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
          mpSet.remove(mp);
          mp.stop();
          mp.release();
        }
      });
    }

    mpSet.add(mp);
    mp.start();
  }

  /**
   * Stop playing music.
   */
  public static void stop() {
    for (MediaPlayer mp : mpSet) {
      if (mp != null) {
        mp.stop();
        mp.release();
      }
    }
    mpSet.clear();
  }

  /**
   * Pauses all playing music, call resumeAll to continue playing music.
   */
  public static void pauseAll() {
    for (MediaPlayer mp : mpSet) {
      if (mp != null) {
        mp.pause();
      }
    }
  }

  /**
   * Resumes playing all paused music.
   */
  public static void resumeAll() {
    for (MediaPlayer mp : mpSet) {
      if (mp != null) {
        mp.start();
      }
    }
  }
}




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