Guest User

Untitled

a guest
Sep 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. package com.tomdignan.soundboard2;
  2.  
  3. import java.util.HashMap;
  4. import android.content.Context;
  5. import android.media.AudioManager;
  6. import android.media.SoundPool;
  7. import android.util.Log;
  8.  
  9. public class SoundManager implements SoundPool.OnLoadCompleteListener {
  10. /** SoundPool left volume */
  11. private static final float LEFT_VOLUME = 1.0f;
  12.  
  13. /** SoundPool right volume */
  14. private static final float RIGHT_VOLUME = 1.0f;
  15.  
  16. /** All sounds will have equal priority */
  17. private static final int STREAM_PRIORITY = 0;
  18.  
  19. /** Potential LOOP_MODE */
  20. private static final int MODE_NO_LOOP = 0;
  21.  
  22. /** Potential LOOP_MODE */
  23. @SuppressWarnings("unused")
  24. private static final int MODE_LOOP_FOREVER = -1;
  25.  
  26. /** Whether sounds should loop */
  27. private static final int LOOP_MODE = MODE_NO_LOOP;
  28.  
  29. /** SoundPool playback rate */
  30. private static final float PLAYBACK_RATE = 1.0f;
  31.  
  32. private static final String TAG = "SoundManager";
  33.  
  34. /** Inner SoundManager instance */
  35. private static SoundManager sInstance = null;
  36.  
  37. /** Mapping of resource ids to sound ids returned by load() */
  38. private HashMap<Integer, Integer> mSoundMap = new HashMap<Integer,Integer>();
  39.  
  40. /** SoundPool instance */
  41. private SoundPool mSoundPool;
  42.  
  43. /** Application Context */
  44. private Context mContext;
  45.  
  46. /** Maximum concurrent streams that can play */
  47. private static final int MAX_STREAMS = 2;
  48.  
  49. /** Private constructor for singleton */
  50. private SoundManager(Context context) {
  51. mContext = context.getApplicationContext();
  52. mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_SYSTEM, 0);
  53. mSoundPool.setOnLoadCompleteListener(this);
  54. }
  55.  
  56. /** Static access to internal instance */
  57. public static SoundManager getInstance(Context context) {
  58. if (sInstance == null) {
  59. sInstance = new SoundManager(context.getApplicationContext());
  60. }
  61. return sInstance;
  62. }
  63.  
  64. /** Loads a sound. Called automatically by play() if not already loaded */
  65. public void load(int id) {
  66. mSoundMap.put(id, mSoundPool.load(mContext, id, 1));
  67. }
  68.  
  69. /**
  70. * Test if sound is loaded, call with id from R.raw
  71. *
  72. * @param resourceId
  73. * @return true|false
  74. */
  75. public boolean isSoundLoaded(int resourceId) {
  76. return mSoundMap.containsKey(resourceId);
  77. }
  78.  
  79. /** Unload sound, prints warning if sound is not loaded */
  80. public void unload(int id) {
  81. if (mSoundMap.containsKey(id)) {
  82. int soundId = mSoundMap.remove(id);
  83. mSoundPool.unload(soundId);
  84. } else {
  85. Log.w(TAG, "sound: " + id + " is not loaded!");
  86. }
  87. }
  88.  
  89. public void play(int resourceId) {
  90. if (isSoundLoaded(resourceId)) {
  91. mSoundPool.play(mSoundMap.get(resourceId), LEFT_VOLUME, RIGHT_VOLUME, STREAM_PRIORITY, LOOP_MODE, PLAYBACK_RATE);
  92. } else {
  93. load(resourceId);
  94. }
  95. }
  96.  
  97. /**
  98. * If the sound is being loaded for the first time, we should wait until it
  99. * is completely loaded to play it.
  100. */
  101. @Override
  102. public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  103. mSoundPool.play(sampleId, LEFT_VOLUME, RIGHT_VOLUME, STREAM_PRIORITY, LOOP_MODE, PLAYBACK_RATE);
  104. }
  105. }
Add Comment
Please, Sign In to add comment