@capgo/native-audio 5.1.3 → 5.1.10
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -36
- package/android/src/main/java/ee/forgr/audio/AudioAsset.java +115 -107
- package/android/src/main/java/ee/forgr/audio/AudioDispatcher.java +185 -168
- package/android/src/main/java/ee/forgr/audio/Constant.java +14 -13
- package/android/src/main/java/ee/forgr/audio/NativeAudio.java +430 -405
- package/dist/docs.json +26 -26
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +4 -4
- package/dist/esm/web.d.ts +3 -3
- package/dist/esm/web.js +20 -19
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +19 -18
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +19 -18
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/AudioAsset.swift +38 -45
- package/ios/Plugin/Constant.swift +1 -0
- package/ios/Plugin/Plugin.swift +15 -11
- package/package.json +14 -13
@@ -33,458 +33,483 @@ import java.util.HashMap;
|
|
33
33
|
import java.util.concurrent.Callable;
|
34
34
|
|
35
35
|
@CapacitorPlugin(
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
permissions = {
|
37
|
+
@Permission(strings = { Manifest.permission.MODIFY_AUDIO_SETTINGS }),
|
38
|
+
@Permission(strings = { Manifest.permission.WRITE_EXTERNAL_STORAGE }),
|
39
|
+
@Permission(strings = { Manifest.permission.READ_PHONE_STATE }),
|
40
|
+
}
|
41
41
|
)
|
42
|
-
public class NativeAudio
|
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
|
-
|
42
|
+
public class NativeAudio
|
43
|
+
extends Plugin
|
44
|
+
implements AudioManager.OnAudioFocusChangeListener {
|
45
|
+
|
46
|
+
public static final String TAG = "NativeAudio";
|
47
|
+
|
48
|
+
private static HashMap<String, AudioAsset> audioAssetList;
|
49
|
+
private static ArrayList<AudioAsset> resumeList;
|
50
|
+
private boolean fadeMusic = false;
|
51
|
+
private AudioManager audioManager;
|
52
|
+
|
53
|
+
@Override
|
54
|
+
public void load() {
|
55
|
+
super.load();
|
56
|
+
|
57
|
+
this.audioManager =
|
58
|
+
(AudioManager) getBridge()
|
59
|
+
.getActivity()
|
60
|
+
.getSystemService(Context.AUDIO_SERVICE);
|
61
|
+
}
|
62
|
+
|
63
|
+
@Override
|
64
|
+
public void onAudioFocusChange(int focusChange) {
|
65
|
+
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {} else if (
|
66
|
+
focusChange == AudioManager.AUDIOFOCUS_GAIN
|
67
|
+
) {} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {}
|
68
|
+
}
|
69
|
+
|
70
|
+
@Override
|
71
|
+
protected void handleOnPause() {
|
72
|
+
super.handleOnPause();
|
73
|
+
|
74
|
+
try {
|
75
|
+
if (audioAssetList != null) {
|
76
|
+
for (HashMap.Entry<String, AudioAsset> entry : audioAssetList.entrySet()) {
|
77
|
+
AudioAsset audio = entry.getValue();
|
78
|
+
|
79
|
+
if (audio != null) {
|
80
|
+
boolean wasPlaying = audio.pause();
|
81
|
+
|
82
|
+
if (wasPlaying) {
|
83
|
+
resumeList.add(audio);
|
82
84
|
}
|
83
|
-
|
84
|
-
Log.d(TAG, "Exception caught while listening for handleOnPause: " + ex.getLocalizedMessage());
|
85
|
+
}
|
85
86
|
}
|
87
|
+
}
|
88
|
+
} catch (Exception ex) {
|
89
|
+
Log.d(
|
90
|
+
TAG,
|
91
|
+
"Exception caught while listening for handleOnPause: " +
|
92
|
+
ex.getLocalizedMessage()
|
93
|
+
);
|
86
94
|
}
|
95
|
+
}
|
87
96
|
|
88
|
-
|
89
|
-
|
90
|
-
|
97
|
+
@Override
|
98
|
+
protected void handleOnResume() {
|
99
|
+
super.handleOnResume();
|
91
100
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
101
|
+
try {
|
102
|
+
if (resumeList != null) {
|
103
|
+
while (!resumeList.isEmpty()) {
|
104
|
+
AudioAsset audio = resumeList.remove(0);
|
96
105
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
}
|
101
|
-
}
|
102
|
-
} catch (Exception ex) {
|
103
|
-
Log.d(TAG, "Exception caught while listening for handleOnResume: " + ex.getLocalizedMessage());
|
106
|
+
if (audio != null) {
|
107
|
+
audio.resume();
|
108
|
+
}
|
104
109
|
}
|
110
|
+
}
|
111
|
+
} catch (Exception ex) {
|
112
|
+
Log.d(
|
113
|
+
TAG,
|
114
|
+
"Exception caught while listening for handleOnResume: " +
|
115
|
+
ex.getLocalizedMessage()
|
116
|
+
);
|
105
117
|
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
new Runnable() {
|
126
|
-
@Override
|
127
|
-
public void run() {
|
128
|
-
preloadAsset(call);
|
129
|
-
}
|
130
|
-
}
|
131
|
-
)
|
132
|
-
.start();
|
133
|
-
}
|
134
|
-
|
135
|
-
@PluginMethod
|
136
|
-
public void play(final PluginCall call) {
|
137
|
-
getBridge()
|
138
|
-
.getActivity()
|
139
|
-
.runOnUiThread(
|
140
|
-
new Runnable() {
|
141
|
-
@Override
|
142
|
-
public void run() {
|
143
|
-
playOrLoop("play", call);
|
144
|
-
}
|
145
|
-
}
|
146
|
-
);
|
118
|
+
}
|
119
|
+
|
120
|
+
@PluginMethod
|
121
|
+
public void configure(PluginCall call) {
|
122
|
+
initSoundPool();
|
123
|
+
|
124
|
+
if (call.hasOption(OPT_FADE_MUSIC)) this.fadeMusic =
|
125
|
+
call.getBoolean(OPT_FADE_MUSIC);
|
126
|
+
|
127
|
+
if (call.hasOption(OPT_FOCUS_AUDIO) && this.audioManager != null) {
|
128
|
+
if (call.getBoolean(OPT_FOCUS_AUDIO)) {
|
129
|
+
this.audioManager.requestAudioFocus(
|
130
|
+
this,
|
131
|
+
AudioManager.STREAM_MUSIC,
|
132
|
+
AudioManager.AUDIOFOCUS_GAIN
|
133
|
+
);
|
134
|
+
} else {
|
135
|
+
this.audioManager.abandonAudioFocus(this);
|
136
|
+
}
|
147
137
|
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
138
|
+
}
|
139
|
+
|
140
|
+
@PluginMethod
|
141
|
+
public void preload(final PluginCall call) {
|
142
|
+
new Thread(
|
143
|
+
new Runnable() {
|
144
|
+
@Override
|
145
|
+
public void run() {
|
146
|
+
preloadAsset(call);
|
147
|
+
}
|
148
|
+
}
|
149
|
+
)
|
150
|
+
.start();
|
151
|
+
}
|
152
|
+
|
153
|
+
@PluginMethod
|
154
|
+
public void play(final PluginCall call) {
|
155
|
+
getBridge()
|
156
|
+
.getActivity()
|
157
|
+
.runOnUiThread(
|
158
|
+
new Runnable() {
|
159
|
+
@Override
|
160
|
+
public void run() {
|
161
|
+
playOrLoop("play", call);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
);
|
165
|
+
}
|
166
|
+
|
167
|
+
@PluginMethod
|
168
|
+
public void getCurrentTime(final PluginCall call) {
|
169
|
+
try {
|
170
|
+
initSoundPool();
|
171
|
+
|
172
|
+
String audioId = call.getString(ASSET_ID);
|
173
|
+
|
174
|
+
if (!isStringValid(audioId)) {
|
175
|
+
call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
|
176
|
+
return;
|
177
|
+
}
|
178
|
+
|
179
|
+
if (audioAssetList.containsKey(audioId)) {
|
180
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
181
|
+
if (asset != null) {
|
182
|
+
call.resolve(
|
183
|
+
new JSObject().put("currentTime", asset.getCurrentPosition())
|
184
|
+
);
|
171
185
|
}
|
186
|
+
} else {
|
187
|
+
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
188
|
+
}
|
189
|
+
} catch (Exception ex) {
|
190
|
+
call.reject(ex.getMessage());
|
172
191
|
}
|
192
|
+
}
|
173
193
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
194
|
+
@PluginMethod
|
195
|
+
public void getDuration(final PluginCall call) {
|
196
|
+
try {
|
197
|
+
initSoundPool();
|
178
198
|
|
179
|
-
|
199
|
+
String audioId = call.getString(ASSET_ID);
|
180
200
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
201
|
+
if (!isStringValid(audioId)) {
|
202
|
+
call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
|
203
|
+
return;
|
204
|
+
}
|
185
205
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
}
|
191
|
-
} else {
|
192
|
-
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
193
|
-
}
|
194
|
-
} catch (Exception ex) {
|
195
|
-
call.reject(ex.getMessage());
|
206
|
+
if (audioAssetList.containsKey(audioId)) {
|
207
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
208
|
+
if (asset != null) {
|
209
|
+
call.resolve(new JSObject().put("duration", asset.getDuration()));
|
196
210
|
}
|
211
|
+
} else {
|
212
|
+
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
213
|
+
}
|
214
|
+
} catch (Exception ex) {
|
215
|
+
call.reject(ex.getMessage());
|
197
216
|
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
217
|
+
}
|
218
|
+
|
219
|
+
@PluginMethod
|
220
|
+
public void loop(final PluginCall call) {
|
221
|
+
getBridge()
|
222
|
+
.getActivity()
|
223
|
+
.runOnUiThread(
|
224
|
+
new Runnable() {
|
225
|
+
@Override
|
226
|
+
public void run() {
|
227
|
+
playOrLoop("loop", call);
|
228
|
+
}
|
229
|
+
}
|
230
|
+
);
|
231
|
+
}
|
232
|
+
|
233
|
+
@PluginMethod
|
234
|
+
public void pause(PluginCall call) {
|
235
|
+
try {
|
236
|
+
initSoundPool();
|
237
|
+
String audioId = call.getString(ASSET_ID);
|
238
|
+
|
239
|
+
if (audioAssetList.containsKey(audioId)) {
|
240
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
241
|
+
if (asset != null) {
|
242
|
+
boolean wasPlaying = asset.pause();
|
243
|
+
|
244
|
+
if (wasPlaying) {
|
245
|
+
resumeList.add(asset);
|
246
|
+
}
|
247
|
+
call.resolve();
|
248
|
+
} else {
|
249
|
+
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
250
|
+
}
|
251
|
+
} else {
|
252
|
+
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
253
|
+
}
|
254
|
+
} catch (Exception ex) {
|
255
|
+
call.reject(ex.getMessage());
|
211
256
|
}
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
} else {
|
229
|
-
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
230
|
-
}
|
231
|
-
} else {
|
232
|
-
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
233
|
-
}
|
234
|
-
} catch (Exception ex) {
|
235
|
-
call.reject(ex.getMessage());
|
257
|
+
}
|
258
|
+
|
259
|
+
@PluginMethod
|
260
|
+
public void resume(PluginCall call) {
|
261
|
+
try {
|
262
|
+
initSoundPool();
|
263
|
+
String audioId = call.getString(ASSET_ID);
|
264
|
+
|
265
|
+
if (audioAssetList.containsKey(audioId)) {
|
266
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
267
|
+
if (asset != null) {
|
268
|
+
asset.resume();
|
269
|
+
resumeList.add(asset);
|
270
|
+
call.resolve();
|
271
|
+
} else {
|
272
|
+
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
236
273
|
}
|
274
|
+
} else {
|
275
|
+
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
276
|
+
}
|
277
|
+
} catch (Exception ex) {
|
278
|
+
call.reject(ex.getMessage());
|
237
279
|
}
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
}
|
254
|
-
} else {
|
255
|
-
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
256
|
-
}
|
257
|
-
} catch (Exception ex) {
|
258
|
-
call.reject(ex.getMessage());
|
280
|
+
}
|
281
|
+
|
282
|
+
@PluginMethod
|
283
|
+
public void stop(PluginCall call) {
|
284
|
+
try {
|
285
|
+
initSoundPool();
|
286
|
+
String audioId = call.getString(ASSET_ID);
|
287
|
+
|
288
|
+
if (audioAssetList.containsKey(audioId)) {
|
289
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
290
|
+
if (asset != null) {
|
291
|
+
asset.stop();
|
292
|
+
call.resolve();
|
293
|
+
} else {
|
294
|
+
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
259
295
|
}
|
296
|
+
} else {
|
297
|
+
call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
|
298
|
+
}
|
299
|
+
} catch (Exception ex) {
|
300
|
+
call.reject(ex.getMessage());
|
260
301
|
}
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
call.
|
302
|
+
}
|
303
|
+
|
304
|
+
@PluginMethod
|
305
|
+
public void unload(PluginCall call) {
|
306
|
+
try {
|
307
|
+
initSoundPool();
|
308
|
+
new JSObject();
|
309
|
+
JSObject status;
|
310
|
+
|
311
|
+
if (isStringValid(call.getString(ASSET_ID))) {
|
312
|
+
String audioId = call.getString(ASSET_ID);
|
313
|
+
|
314
|
+
if (audioAssetList.containsKey(audioId)) {
|
315
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
316
|
+
if (asset != null) {
|
317
|
+
asset.unload();
|
318
|
+
audioAssetList.remove(audioId);
|
319
|
+
status = new JSObject();
|
320
|
+
status.put("status", "OK");
|
321
|
+
call.resolve(status);
|
322
|
+
} else {
|
323
|
+
status = new JSObject();
|
324
|
+
status.put("status", false);
|
325
|
+
call.resolve(status);
|
326
|
+
}
|
327
|
+
} else {
|
328
|
+
status = new JSObject();
|
329
|
+
status.put("status", ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
330
|
+
call.resolve(status);
|
281
331
|
}
|
332
|
+
} else {
|
333
|
+
status = new JSObject();
|
334
|
+
status.put("status", ERROR_AUDIO_ID_MISSING);
|
335
|
+
call.resolve(status);
|
336
|
+
}
|
337
|
+
} catch (Exception ex) {
|
338
|
+
call.reject(ex.getMessage());
|
282
339
|
}
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
status.put("status", "OK");
|
301
|
-
call.resolve(status);
|
302
|
-
} else {
|
303
|
-
status = new JSObject();
|
304
|
-
status.put("status", false);
|
305
|
-
call.resolve(status);
|
306
|
-
}
|
307
|
-
} else {
|
308
|
-
status = new JSObject();
|
309
|
-
status.put("status", ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
310
|
-
call.resolve(status);
|
311
|
-
}
|
312
|
-
} else {
|
313
|
-
status = new JSObject();
|
314
|
-
status.put("status", ERROR_AUDIO_ID_MISSING);
|
315
|
-
call.resolve(status);
|
316
|
-
}
|
317
|
-
} catch (Exception ex) {
|
318
|
-
call.reject(ex.getMessage());
|
340
|
+
}
|
341
|
+
|
342
|
+
@PluginMethod
|
343
|
+
public void setVolume(PluginCall call) {
|
344
|
+
try {
|
345
|
+
initSoundPool();
|
346
|
+
|
347
|
+
String audioId = call.getString(ASSET_ID);
|
348
|
+
float volume = call.getFloat(VOLUME);
|
349
|
+
|
350
|
+
if (audioAssetList.containsKey(audioId)) {
|
351
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
352
|
+
if (asset != null) {
|
353
|
+
asset.setVolume(volume);
|
354
|
+
call.resolve();
|
355
|
+
} else {
|
356
|
+
call.reject(ERROR_AUDIO_ASSET_MISSING);
|
319
357
|
}
|
358
|
+
} else {
|
359
|
+
call.reject(ERROR_AUDIO_ASSET_MISSING);
|
360
|
+
}
|
361
|
+
} catch (Exception ex) {
|
362
|
+
call.reject(ex.getMessage());
|
320
363
|
}
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
} catch (Exception ex) {
|
342
|
-
call.reject(ex.getMessage());
|
364
|
+
}
|
365
|
+
|
366
|
+
@PluginMethod
|
367
|
+
public void isPlaying(final PluginCall call) {
|
368
|
+
try {
|
369
|
+
initSoundPool();
|
370
|
+
|
371
|
+
String audioId = call.getString(ASSET_ID);
|
372
|
+
|
373
|
+
if (!isStringValid(audioId)) {
|
374
|
+
call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
|
375
|
+
return;
|
376
|
+
}
|
377
|
+
|
378
|
+
if (audioAssetList.containsKey(audioId)) {
|
379
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
380
|
+
if (asset != null) {
|
381
|
+
call.resolve(new JSObject().put("isPlaying", asset.isPlaying()));
|
382
|
+
} else {
|
383
|
+
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
343
384
|
}
|
385
|
+
} else {
|
386
|
+
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
387
|
+
}
|
388
|
+
} catch (Exception ex) {
|
389
|
+
call.reject(ex.getMessage());
|
344
390
|
}
|
391
|
+
}
|
345
392
|
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
393
|
+
public void dispatchComplete(String assetId) {
|
394
|
+
JSObject ret = new JSObject();
|
395
|
+
ret.put("assetId", assetId);
|
396
|
+
notifyListeners("complete", ret);
|
397
|
+
}
|
350
398
|
|
351
|
-
|
399
|
+
private void preloadAsset(PluginCall call) {
|
400
|
+
double volume = 1.0;
|
401
|
+
int audioChannelNum = 1;
|
352
402
|
|
353
|
-
|
354
|
-
|
355
|
-
return;
|
356
|
-
}
|
403
|
+
try {
|
404
|
+
initSoundPool();
|
357
405
|
|
358
|
-
|
359
|
-
AudioAsset asset = audioAssetList.get(audioId);
|
360
|
-
if (asset != null) {
|
361
|
-
call.resolve(new JSObject().put("isPlaying", asset.isPlaying()));
|
362
|
-
} else {
|
363
|
-
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
364
|
-
}
|
365
|
-
} else {
|
366
|
-
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
|
367
|
-
}
|
368
|
-
} catch (Exception ex) {
|
369
|
-
call.reject(ex.getMessage());
|
370
|
-
}
|
371
|
-
}
|
372
|
-
|
373
|
-
public void dispatchComplete(String assetId) {
|
374
|
-
JSObject ret = new JSObject();
|
375
|
-
ret.put("assetId", assetId);
|
376
|
-
notifyListeners("complete", ret);
|
377
|
-
}
|
406
|
+
String audioId = call.getString(ASSET_ID);
|
378
407
|
|
379
|
-
|
380
|
-
double volume = 1.0;
|
381
|
-
int audioChannelNum = 1;
|
408
|
+
boolean isUrl = call.getBoolean("isUrl", false);
|
382
409
|
|
383
|
-
|
384
|
-
|
410
|
+
if (!isStringValid(audioId)) {
|
411
|
+
call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
|
412
|
+
return;
|
413
|
+
}
|
385
414
|
|
386
|
-
|
415
|
+
if (!audioAssetList.containsKey(audioId)) {
|
416
|
+
String assetPath = call.getString(ASSET_PATH);
|
387
417
|
|
388
|
-
|
418
|
+
if (!isStringValid(assetPath)) {
|
419
|
+
call.reject(
|
420
|
+
ERROR_ASSET_PATH_MISSING + " - " + audioId + " - " + assetPath
|
421
|
+
);
|
422
|
+
return;
|
423
|
+
}
|
389
424
|
|
390
|
-
|
391
|
-
call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
|
392
|
-
return;
|
393
|
-
}
|
425
|
+
String fullPath = assetPath; //"raw/".concat(assetPath);
|
394
426
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
call.reject(ERROR_ASSET_PATH_MISSING + " - " + audioId + " - " + assetPath);
|
400
|
-
return;
|
401
|
-
}
|
402
|
-
|
403
|
-
String fullPath = assetPath; //"raw/".concat(assetPath);
|
404
|
-
|
405
|
-
if (call.getDouble(VOLUME) == null) {
|
406
|
-
volume = 1.0;
|
407
|
-
} else {
|
408
|
-
volume = call.getDouble(VOLUME, 0.5);
|
409
|
-
}
|
410
|
-
|
411
|
-
if (call.getInt(AUDIO_CHANNEL_NUM) == null) {
|
412
|
-
audioChannelNum = 1;
|
413
|
-
} else {
|
414
|
-
audioChannelNum = call.getInt(AUDIO_CHANNEL_NUM);
|
415
|
-
}
|
416
|
-
|
417
|
-
AssetFileDescriptor assetFileDescriptor;
|
418
|
-
if (isUrl) {
|
419
|
-
File f = new File(new URI(fullPath));
|
420
|
-
ParcelFileDescriptor p = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
|
421
|
-
assetFileDescriptor = new AssetFileDescriptor(p, 0, -1);
|
422
|
-
} else {
|
423
|
-
// if fullPath dont start with public/ add it
|
424
|
-
if (!fullPath.startsWith("public/")) {
|
425
|
-
fullPath = "public/".concat(fullPath);
|
426
|
-
}
|
427
|
-
Context ctx = getBridge().getActivity().getApplicationContext();
|
428
|
-
AssetManager am = ctx.getResources().getAssets();
|
429
|
-
assetFileDescriptor = am.openFd(fullPath);
|
430
|
-
}
|
431
|
-
|
432
|
-
AudioAsset asset = new AudioAsset(this, audioId, assetFileDescriptor, audioChannelNum, (float) volume);
|
433
|
-
audioAssetList.put(audioId, asset);
|
434
|
-
|
435
|
-
JSObject status = new JSObject();
|
436
|
-
status.put("STATUS", "OK");
|
437
|
-
call.resolve(status);
|
438
|
-
} else {
|
439
|
-
call.reject(ERROR_AUDIO_EXISTS);
|
440
|
-
}
|
441
|
-
} catch (Exception ex) {
|
442
|
-
call.reject(ex.getMessage());
|
427
|
+
if (call.getDouble(VOLUME) == null) {
|
428
|
+
volume = 1.0;
|
429
|
+
} else {
|
430
|
+
volume = call.getDouble(VOLUME, 0.5);
|
443
431
|
}
|
444
|
-
}
|
445
432
|
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
final String audioId = call.getString(ASSET_ID);
|
451
|
-
final Double time = call.getDouble("time", 0.0);
|
452
|
-
if (audioAssetList.containsKey(audioId)) {
|
453
|
-
AudioAsset asset = audioAssetList.get(audioId);
|
454
|
-
if (LOOP.equals(action) && asset != null) {
|
455
|
-
asset.loop();
|
456
|
-
call.resolve();
|
457
|
-
} else if (asset != null) {
|
458
|
-
asset.play(
|
459
|
-
time,
|
460
|
-
new Callable<Void>() {
|
461
|
-
@Override
|
462
|
-
public Void call() throws Exception {
|
463
|
-
call.resolve(new JSObject().put(ASSET_ID, audioId));
|
464
|
-
return null;
|
465
|
-
}
|
466
|
-
}
|
467
|
-
);
|
468
|
-
} else {
|
469
|
-
call.reject("Error with asset");
|
470
|
-
}
|
471
|
-
}
|
472
|
-
} catch (Exception ex) {
|
473
|
-
call.reject(ex.getMessage());
|
433
|
+
if (call.getInt(AUDIO_CHANNEL_NUM) == null) {
|
434
|
+
audioChannelNum = 1;
|
435
|
+
} else {
|
436
|
+
audioChannelNum = call.getInt(AUDIO_CHANNEL_NUM);
|
474
437
|
}
|
475
|
-
}
|
476
438
|
|
477
|
-
|
478
|
-
if (
|
479
|
-
|
439
|
+
AssetFileDescriptor assetFileDescriptor;
|
440
|
+
if (isUrl) {
|
441
|
+
File f = new File(new URI(fullPath));
|
442
|
+
ParcelFileDescriptor p = ParcelFileDescriptor.open(
|
443
|
+
f,
|
444
|
+
ParcelFileDescriptor.MODE_READ_ONLY
|
445
|
+
);
|
446
|
+
assetFileDescriptor = new AssetFileDescriptor(p, 0, -1);
|
447
|
+
} else {
|
448
|
+
// if fullPath dont start with public/ add it
|
449
|
+
if (!fullPath.startsWith("public/")) {
|
450
|
+
fullPath = "public/".concat(fullPath);
|
451
|
+
}
|
452
|
+
Context ctx = getBridge().getActivity().getApplicationContext();
|
453
|
+
AssetManager am = ctx.getResources().getAssets();
|
454
|
+
assetFileDescriptor = am.openFd(fullPath);
|
480
455
|
}
|
481
456
|
|
482
|
-
|
483
|
-
|
457
|
+
AudioAsset asset = new AudioAsset(
|
458
|
+
this,
|
459
|
+
audioId,
|
460
|
+
assetFileDescriptor,
|
461
|
+
audioChannelNum,
|
462
|
+
(float) volume
|
463
|
+
);
|
464
|
+
audioAssetList.put(audioId, asset);
|
465
|
+
|
466
|
+
JSObject status = new JSObject();
|
467
|
+
status.put("STATUS", "OK");
|
468
|
+
call.resolve(status);
|
469
|
+
} else {
|
470
|
+
call.reject(ERROR_AUDIO_EXISTS);
|
471
|
+
}
|
472
|
+
} catch (Exception ex) {
|
473
|
+
call.reject(ex.getMessage());
|
474
|
+
}
|
475
|
+
}
|
476
|
+
|
477
|
+
private void playOrLoop(String action, final PluginCall call) {
|
478
|
+
try {
|
479
|
+
initSoundPool();
|
480
|
+
|
481
|
+
final String audioId = call.getString(ASSET_ID);
|
482
|
+
final Double time = call.getDouble("time", 0.0);
|
483
|
+
if (audioAssetList.containsKey(audioId)) {
|
484
|
+
AudioAsset asset = audioAssetList.get(audioId);
|
485
|
+
if (LOOP.equals(action) && asset != null) {
|
486
|
+
asset.loop();
|
487
|
+
call.resolve();
|
488
|
+
} else if (asset != null) {
|
489
|
+
asset.play(time);
|
490
|
+
call.resolve();
|
491
|
+
} else {
|
492
|
+
call.reject("Error with asset");
|
484
493
|
}
|
494
|
+
} else {
|
495
|
+
call.reject("Error with asset");
|
496
|
+
}
|
497
|
+
} catch (Exception ex) {
|
498
|
+
call.reject(ex.getMessage());
|
485
499
|
}
|
500
|
+
}
|
486
501
|
|
487
|
-
|
488
|
-
|
502
|
+
private void initSoundPool() {
|
503
|
+
if (audioAssetList == null) {
|
504
|
+
audioAssetList = new HashMap<>();
|
489
505
|
}
|
506
|
+
|
507
|
+
if (resumeList == null) {
|
508
|
+
resumeList = new ArrayList<>();
|
509
|
+
}
|
510
|
+
}
|
511
|
+
|
512
|
+
private boolean isStringValid(String value) {
|
513
|
+
return (value != null && !value.isEmpty() && !value.equals("null"));
|
514
|
+
}
|
490
515
|
}
|