/
SoundManager.java
206 lines (171 loc) · 5.34 KB
/
SoundManager.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package im.delight.android.audio;
/*
* Copyright (c) delight.im <info@delight.im>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.annotation.SuppressLint;
import android.content.Context;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import android.media.AudioManager;
import java.util.Map;
import android.media.SoundPool;
/** Plays sounds (frequently and fast) while managing the resources efficiently */
@SuppressLint("UseSparseArrays")
public class SoundManager extends Thread {
private static class SoundManagerTask {
private static final int ACTION_LOAD = 1;
private static final int ACTION_PLAY = 2;
private static final int ACTION_UNLOAD = 3;
private static final int ACTION_CANCEL = 4;
private final int mSoundResourceId;
private final float mVolume;
private final int mRepetitions;
private final int mAction;
private SoundManagerTask(final int soundResourceId, final float volume, final int repetitions, final int action) {
mSoundResourceId = soundResourceId;
mVolume = volume;
mRepetitions = repetitions;
mAction = action;
}
public static SoundManagerTask load(final int soundResourceId) {
return new SoundManagerTask(soundResourceId, 0, 0, ACTION_LOAD);
}
public static SoundManagerTask play(final int soundResourceId, final float volume, final int repetitions) {
return new SoundManagerTask(soundResourceId, volume, repetitions, ACTION_PLAY);
}
public static SoundManagerTask unload(final int soundResourceId) {
return new SoundManagerTask(soundResourceId, 0, 0, ACTION_UNLOAD);
}
public static SoundManagerTask cancel() {
return new SoundManagerTask(0, 0, 0, ACTION_CANCEL);
}
public int getSoundResourceId() {
return mSoundResourceId;
}
public float getVolume() {
return mVolume;
}
public int getRepetitions() {
return mRepetitions;
}
public boolean isLoad() {
return mAction == ACTION_LOAD;
}
public boolean isPlay() {
return mAction == ACTION_PLAY;
}
public boolean isUnload() {
return mAction == ACTION_UNLOAD;
}
public boolean isCancel() {
return mAction == ACTION_CANCEL;
}
}
private final SoundPool mSoundPool;
private final Context mContext;
private final Map<Integer, Integer> mSounds;
private final BlockingQueue<SoundManagerTask> mTasks = new LinkedBlockingQueue<SoundManagerTask>();
private volatile boolean mCancelled;
@SuppressWarnings("deprecation")
public SoundManager(final Context context, final int maxSimultaneousStreams) {
mSoundPool = new SoundPool(maxSimultaneousStreams, AudioManager.STREAM_MUSIC, 0);
mContext = context.getApplicationContext();
mSounds = new HashMap<Integer, Integer>();
}
public void load(final int soundResourceId) {
try {
mTasks.put(SoundManagerTask.load(soundResourceId));
}
catch (InterruptedException e) { }
}
public void play(final int soundResourceId) {
play(soundResourceId, 1.0f);
}
public void play(final int soundResourceId, final float volume) {
play(soundResourceId, volume, 0);
}
public void play(final int soundResourceId, final float volume, final int repetitions) {
if (!isAlive()) {
return;
}
try {
mTasks.put(SoundManagerTask.play(soundResourceId, volume, repetitions));
}
catch (InterruptedException e) { }
}
public void unload(final int soundResourceId) {
try {
mTasks.put(SoundManagerTask.unload(soundResourceId));
}
catch (InterruptedException e) { }
}
public void cancel() {
try {
mTasks.put(SoundManagerTask.cancel());
}
catch (InterruptedException e) { }
}
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
try {
SoundManagerTask task;
while (!mCancelled) {
task = mTasks.take();
if (task.isCancel()) {
mCancelled = true;
break;
}
else {
final Integer currentMapping;
synchronized (mSounds) {
currentMapping = mSounds.get(task.getSoundResourceId());
}
if (task.isLoad()) {
if (currentMapping == null) {
final int newMapping = mSoundPool.load(mContext, task.getSoundResourceId(), 1);
synchronized (mSounds) {
mSounds.put(task.getSoundResourceId(), newMapping);
}
}
}
else if (task.isPlay()) {
if (currentMapping != null) {
mSoundPool.play(currentMapping.intValue(), task.getVolume(), task.getVolume(), 0, task.getRepetitions(), 1.0f);
}
}
else if (task.isUnload()) {
if (currentMapping != null) {
mSoundPool.unload(currentMapping.intValue());
synchronized (mSounds) {
mSounds.remove(task.getSoundResourceId());
}
}
}
}
}
}
catch (InterruptedException e) { }
if (mSounds != null) {
synchronized (mSounds) {
mSounds.clear();
}
}
if (mSoundPool != null) {
mSoundPool.release();
}
}
}