Back to project page Do-not-get-annoyed.
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.
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); } }