Android Open Source - Do-not-get-annoyed Sound Service






From Project

Back to project page Do-not-get-annoyed.

License

The source code is released under:

Apache License

If you think the Android project Do-not-get-annoyed 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 mn100013d.pmu.services;
/*ww  w  .ja v a2 s. c o m*/
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import mn100013d.pmu.R;
import mn100013d.pmu.exceptions.ContextNotSetException;
import mn100013d.pmu.exceptions.PlayerNotRegisteredException;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class SoundService {
  private static final String WARNING_TAG = "mn100013d.pmu.services.WARNING";
  private static final String INFO_TAG = "mn100013d.pmu.services.INFO";
  
  public static final String JINGLE = "mn100013d.pmu.services.SoundService.JINGLE";
  public static final String CLICK_SOUND = "mn100013d.pmu.services.SoundService.CLICK_SOUND";
  public static final String GAME_START = "mn100013d.pmu.services.SoundService.GAME_START";
  public static final String INGAME_TRACK = "mn100013d.pmu.services.SoundService.INGAME_TRACK";
  public static final String PLAYER_MOVE_1 = "mn100013d.pmu.services.SoundService.PLAYER_MOVE_1";
  public static final String PLAYER_MOVE_2 = "mn100013d.pmu.services.SoundService.PLAYER_MOVE_2";
  public static final String DICE_ROLL = "mn100013d.pmu.services.SoundService.DICE_ROLL";
  public static final String DICE_THROW = "mn100013d.pmu.services.SoundService.DICE_THROW";
  public static final String TP = "mn100013d.pmu.services.SoundService.TP";
  
  private static SoundService instance = null;
  private Context context;
  private Map<String, MediaPlayer> mPlayers = new HashMap<String, MediaPlayer>();
  
  protected SoundService() {
  }
  public void instantiateService(){
    if (context == null){
      return;
    }
    mPlayers.put(JINGLE, MediaPlayer.create(context, R.raw.jingle));
    mPlayers.put(CLICK_SOUND, MediaPlayer.create(context, R.raw.click));
    mPlayers.put(GAME_START, MediaPlayer.create(context, R.raw.startgame));
    mPlayers.put(INGAME_TRACK, MediaPlayer.create(context, R.raw.ingame));
    mPlayers.put(PLAYER_MOVE_1, MediaPlayer.create(context, R.raw.piung));
    mPlayers.put(PLAYER_MOVE_2, MediaPlayer.create(context, R.raw.poing));
    mPlayers.put(DICE_ROLL, MediaPlayer.create(context, R.raw.rolling));
    mPlayers.put(DICE_THROW, MediaPlayer.create(context, R.raw.throwing));  
    mPlayers.put(TP, MediaPlayer.create(context, R.raw.move));  
  }
  public static SoundService getInstance() {
    if (instance == null) {
      instance = new SoundService();
    }
    return instance;
  }
  
  public void setContext(Context context) {
    this.context = context;
  }

  public void registerMediaPlayer(int resid, String name) throws ContextNotSetException{
    if (context == null) {
      Log.w(WARNING_TAG,
          "SoundService context is not set! Please set SoundService context using setContext() method");
      throw new ContextNotSetException();
    }
    MediaPlayer mPlayer = MediaPlayer.create(context, resid);
    mPlayers.put(name, mPlayer);
  }
  public void unregisterMediaPlayer(String name){
    MediaPlayer removedPlayer = mPlayers.remove(name);
    if (removedPlayer == null){
      Log.i(INFO_TAG, "Player with specified name is not registered yet!");
    }
    else{
      Log.i(INFO_TAG, "Specified player succesfully unregistered!");
      removedPlayer.stop();
      removedPlayer.release();
      removedPlayer = null;
    }
  }
  public MediaPlayer play(String name, Boolean looping) throws PlayerNotRegisteredException{
    MediaPlayer player = mPlayers.get(name);
    if (player == null){
      Log.w(WARNING_TAG, "Player with specified name is not registered yet!");
      throw new PlayerNotRegisteredException();
    }
    
    player.start();
    player.setLooping(looping);
    Log.i(INFO_TAG, "Player "+name+" started playing with looping set to "+looping);
    return player;
  }
  public void stop(String name) throws PlayerNotRegisteredException{
    MediaPlayer player = mPlayers.get(name);
    if (player == null){
      Log.w(WARNING_TAG, "Player with specified name is not registered yet!");
      throw new PlayerNotRegisteredException();
    }
    player.stop();
    player.prepareAsync();
  }
  public void setLooping(String name) throws PlayerNotRegisteredException{
    MediaPlayer player = mPlayers.get(name);
    if (player == null){
      Log.w(WARNING_TAG, "Player with specified name is not registered yet!");
      throw new PlayerNotRegisteredException();
    }
    if (player.isLooping()){
      Log.i(INFO_TAG, "Player "+name+" is already looping");
      return;
    }
    player.setLooping(true);
  }
  
}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java