1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| public class SoundManager { private static String TAG = SoundManager.class.getSimpleName(); private static SoundManager instance; private int mCurrentStreamId; private Context mContext; private Handler mHandler = new Handler();
private Runnable mPlayNext = new Runnable() { public void run() { SoundManager.this.mSoundPool .stop(SoundManager.this.mCurrentStreamId); SoundManager.this.playNextSound(); } }; private boolean mPlaying; private SoundPool mSoundPool; private HashMap<String, Sound> mSoundPoolMap; private Queue<String> mSoundQueue = new LinkedList<>(); private float volume = 0.5F;
private SoundManager() { }
public static synchronized SoundManager getInstance() { if (instance == null) { instance = new SoundManager(); } return instance; }
private void playNextSound() { if (!this.mSoundQueue.isEmpty()) { String str = this.mSoundQueue.poll(); Sound sound = this.mSoundPoolMap.get(str); if (sound != null) { this.mCurrentStreamId = this.mSoundPool.play(sound.id, volume, volume, 1, 0, 1.0F); this.mPlaying = true; this.mHandler.postDelayed(this.mPlayNext, sound.time); } } }
public void addSound(String text, int resID, int timeDelayed) { Sound localSound = new Sound(this.mSoundPool.load(this.mContext, resID, 1), timeDelayed); this.mSoundPoolMap.put(text, localSound); }
public void cleanup() { unloadAll(); this.mSoundPool.release(); this.mSoundPool = null; instance = null; }
public void initSounds(Context context) { this.mContext = context; this.mSoundPool = new SoundPool(1, 3, 0); this.mSoundPoolMap = new HashMap<>();
this.mPlaying = false;
addSound("0", R.raw.zero, 378); addSound("1", R.raw.one, 360); addSound("2", R.raw.two, 342); addSound("3", R.raw.three, 386); addSound("4", R.raw.four, 414); addSound("5", R.raw.five, 373); addSound("6", R.raw.six, 378); addSound("7", R.raw.seven, 386); addSound("8", R.raw.eight, 342); addSound("9", R.raw.nine, 396);
addSound("shi", R.raw.shi, 396); addSound("bai", R.raw.bai, 342); addSound("qian", R.raw.qian, 378); addSound("wan", R.raw.wan, 342); addSound("yi", R.raw.yi, 324); addSound("yuan", R.raw.yuan, 378); addSound("dot", R.raw.dot, 360); addSound("cgsk", R.raw.cgsk, 1440); }
public void playSeqSounds(List<String> soundsToPlay) { int length = soundsToPlay.size(); for (int j = 0; ; j++) { if (j >= length) { if (!this.mPlaying) playNextSound(); return; } String str = soundsToPlay.get(j); this.mSoundQueue.add(str); } }
public void playSound(String text) { stopSound(); this.mSoundQueue.add(text); playNextSound(); }
public void stopSound() { this.mHandler.removeCallbacks(this.mPlayNext); this.mSoundQueue.clear(); this.mSoundPool.stop(this.mCurrentStreamId); this.mPlaying = false; }
public void unloadAll() { stopSound(); if (this.mSoundPoolMap.size() > 0) { Iterator<String> localIterator = this.mSoundPoolMap.keySet().iterator(); while (true) { if (!localIterator.hasNext()) { this.mSoundPoolMap.clear(); return; } String str = localIterator.next(); this.mSoundPool.unload(this.mSoundPoolMap.get(str).id); } }
}
private final class Sound { public int id; public int time;
public Sound(int id, int time) { this.id = id; this.time = time; } } }
|