forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundModule.java
More file actions
728 lines (640 loc) · 25.5 KB
/
Copy pathSoundModule.java
File metadata and controls
728 lines (640 loc) · 25.5 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
package com.runrev.android;
import java.util.*;
import java.io.*;
import android.media.MediaPlayer;
import android.util.*;
import android.content.res.*;
import android.os.*;
public class SoundModule
{
////////////////////////////////////////////////////////////////////////////////
// Native Funcitons
////////////////////////////////////////////////////////////////////////////////
public static final String TAG = "revandroid.SoundModule";
public static native void doSoundStopped();
public static native void doSoundFinishedOnChannel(String channel, String sound, long object_handle);
public static native void doSoundReleaseCallbackHandle(long object_handle);
////////////////////////////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////////////////////////////
public static final int k_playback_now = 0;
public static final int k_playback_next = 1;
public static final int k_playback_looping = 2;
public static final int k_channel_status_stopped = 0;
public static final int k_channel_status_paused = 1;
public static final int k_channel_status_playing = 2;
////////////////////////////////////////////////////////////////////////////////
// Member variables
////////////////////////////////////////////////////////////////////////////////
private Engine m_engine;
private HashMap<String, SoundChannel> m_channel_map;
private MediaPlayer m_audio_player;
private int m_audio_volume;
private boolean m_play_audio_player;
////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////
public SoundModule(Engine p_engine)
{
m_engine = p_engine;
m_channel_map = new HashMap();
m_audio_player = new MediaPlayer();
m_audio_volume = 100;
m_play_audio_player = false;
}
////////////////////////////////////////////////////////////////////////////////
// Public API
////////////////////////////////////////////////////////////////////////////////
public void onPause()
{
m_play_audio_player = (m_audio_player . isPlaying());
if (m_play_audio_player)
m_audio_player . pause();
Collection<SoundChannel> t_channels = m_channel_map.values();
if (t_channels != null)
{
Iterator<SoundChannel> t_iterator = t_channels.iterator();
while (t_iterator.hasNext())
t_iterator.next().onPause();
}
}
public void onResume()
{
if (m_play_audio_player)
m_audio_player . start();
Collection<SoundChannel> t_channels = m_channel_map.values();
if (t_channels != null)
{
Iterator<SoundChannel> t_iterator = t_channels.iterator();
while (t_iterator.hasNext())
t_iterator.next().onResume();
}
}
// play <soundFile> [looping]
public boolean playSound(String p_path, boolean p_is_asset, boolean p_loop)
{
if (m_audio_player.isPlaying())
m_audio_player.stop();
m_audio_player.reset();
if (p_path != null && p_path.length() > 0)
{
try
{
if (p_is_asset)
{
AssetFileDescriptor t_descriptor;
t_descriptor = m_engine . getContext() . getAssets() . openFd(p_path);
m_audio_player.setDataSource(t_descriptor.getFileDescriptor(), t_descriptor.getStartOffset(), t_descriptor.getLength());
}
else
{
FileInputStream t_filestream = new FileInputStream(p_path);
m_audio_player.setDataSource(t_filestream.getFD());
}
setPlayLoudness(m_audio_volume);
m_audio_player.setLooping(p_loop);
m_audio_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
doSoundStopped();
}
});
m_audio_player.setOnInfoListener(new MediaPlayer.OnInfoListener()
{
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
return false;
}
});
m_audio_player.setOnErrorListener(new MediaPlayer.OnErrorListener()
{
public boolean onError(MediaPlayer mp, int what, int extra)
{
return false;
}
});
m_audio_player.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
m_audio_player.prepareAsync();
}
catch (Exception e)
{
return false;
}
}
return true;
}
// get the playLoudness
public int getPlayLoudness()
{
return m_audio_volume;
}
// set the playLoudness
public boolean setPlayLoudness(int p_loudness)
{
m_audio_volume = p_loudness;
m_audio_player.setVolume(m_audio_volume / 100.0f, m_audio_volume / 100.0f);
return true;
}
// mobilePlaySoundOnChannel
public boolean playSoundOnChannel(String p_sound, String p_channel, int p_type, String p_sound_path, boolean p_is_asset, long p_callback_handle)
{
SoundChannel t_channel = getSoundChannel(p_channel, true);
if (t_channel != null)
return t_channel.playSound(p_sound, p_type, p_sound_path, p_is_asset, p_callback_handle);
return false;
}
// mobileStopPlayingOnChannel
public boolean stopPlayingOnChannel(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.stop();
return false;
}
// mobilePausePlayingOnChannel
public boolean pausePlayingOnChannel(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.pause();
return false;
}
// mobileResumePlayingOnChannel
public boolean resumePlayingOnChannel(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.resume();
return false;
}
// mobileSetSoundChannelVolume
public boolean setChannelVolume(String p_channel, int p_volume)
{
SoundChannel t_channel = getSoundChannel(p_channel, true);
if (t_channel != null)
return t_channel.setVolume(p_volume);
return false;
}
// mobileSoundChannelVolume
public int getChannelVolume(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.getVolume();
return -1;
}
// mobileSoundOnChannel
public String getSoundOnChannel(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.getSound();
return null;
}
// mobileNextSoundOnChannel
public String getNextSoundOnChannel(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.getNextSound();
return null;
}
// mobileSoundChannelStatus
public int getChannelStatus(String p_channel)
{
SoundChannel t_channel = getSoundChannel(p_channel, false);
if (t_channel != null)
return t_channel.getStatus();
return -1;
}
// mobileSoundChannels
public String getSoundChannels()
{
String t_channel_names = new String();
Set<String> t_channels = m_channel_map.keySet();
if (t_channels != null)
{
Iterator t_iterator = t_channels.iterator();
while (t_iterator.hasNext())
{
if (t_channel_names.length() != 0)
t_channel_names += '\n';
t_channel_names += t_iterator.next();
}
}
return t_channel_names;
}
// mobileDeleteSoundChannel
public boolean deleteSoundChannel(String p_channel)
{
SoundChannel t_channel = m_channel_map.remove(p_channel);
if (t_channel != null)
{
t_channel.release();
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Internals
////////////////////////////////////////////////////////////////////////////////
private SoundChannel getSoundChannel(String p_channel, boolean p_create)
{
SoundChannel t_channel = m_channel_map.get(p_channel);
if (t_channel == null && p_create)
{
t_channel = new SoundChannel(p_channel);
m_channel_map.put(p_channel, t_channel);
}
return t_channel;
}
private class SoundChannel
{
private String m_name;
private int m_volume;
private SoundPlayer m_current_player;
private SoundPlayer m_next_player;
private boolean m_resume_channel;
public SoundChannel(String p_name)
{
m_name = p_name;
m_current_player = null;
m_next_player = null;
m_volume = 100;
m_resume_channel = false;
}
public void release()
{
if (m_current_player != null)
m_current_player.release();
if (m_next_player != null)
m_next_player.release();
}
public void onPause()
{
m_resume_channel = (m_current_player != null && m_current_player.m_player != null && m_current_player.m_player.isPlaying());
if (m_resume_channel)
m_current_player.m_player.pause();
}
public void onResume()
{
if (m_resume_channel && m_current_player != null && m_current_player.m_player != null)
m_current_player.m_player.start();
}
public boolean playSound(String p_sound, int p_type, String p_sound_path, boolean p_is_asset, long p_callback_handle)
{
switch (p_type)
{
case SoundModule.k_playback_now:
case SoundModule.k_playback_looping:
// if we have an existing player, stop its activity and use this to play the new sound
// otherwise create a new player
if (m_current_player != null)
m_current_player.reset();
else
{
m_current_player = new SoundPlayer();
if (m_current_player == null)
return false;
}
// when playing a new sound, any queued sounds are removed from channel
// may aswell remove the next player rather than have it hanging around
if (m_next_player != null)
{
m_next_player.release();
m_next_player = null;
}
return m_current_player.setSound(p_sound, p_type == k_playback_looping, true, p_sound_path, p_is_asset, p_callback_handle);
case SoundModule.k_playback_next:
{
// if there is no current player or the current player is not playing
// then queue the sound on the current player but don't play it (the sound is prepared but the channel is paused)
// otherwise set up the sound on the next player ready for when the current player completes
// To check if the current player is not playing we cannot rely *only* on the isPlaying() method of the MediaPlayer class,
// as this returns false until the player actually starts playing. For our purposes, the current player is "playing" if:
// 1. Either MediaPlayer.isPlaying() returns true
// 2. OR there is a pending and unprepared sound, waiting to be played
SoundPlayer t_player = null;
if (m_current_player != null && !(m_current_player.m_player.isPlaying() || (m_current_player.isPending() && !m_current_player.isPrepared())))
{
m_current_player.reset();
t_player = m_current_player;
}
else if (m_current_player != null && m_next_player != null)
{
m_next_player.reset();
t_player = m_next_player;
}
else
{
t_player = new SoundPlayer();
if (m_current_player == null)
m_current_player = t_player;
else
m_next_player = t_player;
}
if (t_player == null)
return false;
return t_player.setSound(p_sound, false, false, p_sound_path, p_is_asset, p_callback_handle);
}
default:
return false;
}
}
public boolean stop()
{
// when we want to stop a channel, clear the current player (but keep ready for a new sound)
// and completely remove the next player in an effort to minimise the number of unecessary player objects
if (m_current_player != null)
{
m_current_player.reset();
if (m_next_player != null)
{
m_next_player.release();
m_next_player = null;
}
return true;
}
return true;
}
public boolean pause()
{
if (m_current_player != null)
return m_current_player.pause();
return true;
}
public boolean resume()
{
if (m_current_player != null)
return m_current_player.play();
return true;
}
public boolean setVolume(int p_volume)
{
m_volume = p_volume;
if (m_current_player != null)
m_current_player.m_player.setVolume(m_volume / 100.0f, m_volume / 100.0f);
return true;
}
public int getVolume()
{
return m_volume;
}
public String getSound()
{
if (m_current_player != null)
return m_current_player.getSound();
return null;
}
public String getNextSound()
{
if (m_next_player != null)
return m_next_player.getSound();
return null;
}
public int getStatus()
{
// playing: the current player is playing, or there is an unprepared sound waiting to be played
// paused: the current player is not playing but there is a sound ready (or being readied) in the player
// stopped: all other cases
if (m_current_player != null)
{
if (m_current_player.m_player.isPlaying() || (m_current_player.isPending() && !m_current_player.isPrepared()))
return k_channel_status_playing;
else if (!m_current_player.m_player.isPlaying() && (m_current_player.isPrepared() || m_current_player.getSound() != null))
return k_channel_status_paused;
}
return k_channel_status_stopped;
}
private void playerComplete(long p_callback_handle)
{
if (m_current_player != null)
{
doSoundFinishedOnChannel(m_name, m_current_player.getSound(), p_callback_handle);
m_engine.wakeEngineThread();
}
// when the current player finishes playing, start up the next player and remove the current player
// if there is no next player, just reset the current player so that it is in the stopped state
if (m_next_player != null && m_next_player.getSound() != null)
{
m_next_player.play();
if (m_current_player != null)
m_current_player.release();
m_current_player = m_next_player;
m_next_player = null;
}
else if (m_current_player != null)
m_current_player.reset();
}
private class SoundPlayer
{
public MediaPlayer m_player;
private boolean m_prepared;
private boolean m_pending;
private String m_sound;
private long m_callback_handle;
public SoundPlayer()
{
m_prepared = false;
m_pending = false;
m_sound = null;
m_callback_handle = -1;
// MM-2012-10-15: [[ Bug 10437 ]] On the Kindle Fire, use the patched MediaPlayer, which invokes the completion listener at the correct point.
if (android.os.Build.MANUFACTURER.equals("Amazon") && android.os.Build.MODEL.equals("Kindle Fire"))
m_player = new PatchedMediaPlayer();
else
m_player = new MediaPlayer();
if (m_player == null)
return;
m_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer p_player)
{
playerComplete(m_callback_handle);
}
});
m_player.setOnInfoListener(new MediaPlayer.OnInfoListener()
{
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
return false;
}
});
m_player.setOnErrorListener(new MediaPlayer.OnErrorListener()
{
public boolean onError(MediaPlayer mp, int what, int extra)
{
Log.i("revandroid", "MultiChannelSound Error(" + what + " " + extra + ")");
return false;
}
});
m_player.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer p_player)
{
m_prepared = true;
if (m_pending)
{
m_pending = false;
p_player.start();
}
}
});
}
public boolean setSound(String p_sound, boolean p_looping, boolean p_play, String p_sound_path, boolean p_is_asset, long p_callback_handle)
{
m_pending = p_play;
m_prepared = false;
m_sound = p_sound_path;
m_callback_handle = p_callback_handle;
m_player.setLooping(p_looping);
m_player.setVolume(m_volume / 100.0f, m_volume / 100.0f);
try {
if (p_is_asset)
{
AssetFileDescriptor t_descriptor;
t_descriptor = m_engine.getContext().getAssets().openFd(p_sound);
m_player.setDataSource(t_descriptor.getFileDescriptor(), t_descriptor.getStartOffset(), t_descriptor.getLength());
}
else
{
FileInputStream t_filestream = new FileInputStream(p_sound);
m_player.setDataSource(t_filestream.getFD());
}
m_player.prepareAsync();
} catch (Exception e) {
Log.i("revandroid", e.toString());
// IM-2013-11-13: [[ Bug 11428 ]] Unset callback handle to prevent early release of callback object when resetting
m_callback_handle = -1;
reset();
return false;
}
return true;
}
public String getSound()
{
return m_sound;
}
// reset the player, putting it in a freshly created state
public void reset()
{
if (m_player.isPlaying())
m_player.stop();
m_sound = null;
m_pending = false;
m_prepared = false;
m_player.reset();
if (m_callback_handle >= 0)
doSoundReleaseCallbackHandle(m_callback_handle);
m_callback_handle = -1;
}
// release all resources associated with this player
public void release()
{
if (m_player != null)
m_player.release();
m_player = null;
m_sound = null;
if (m_callback_handle >= 0)
doSoundReleaseCallbackHandle(m_callback_handle);
}
public boolean play()
{
// if the sound is not yet prepared, set the player to pending so that when the sound
// is prepared we know to begin playing it
if (m_player != null && !m_player.isPlaying())
{
if (m_prepared)
m_player.start();
else
m_pending = true;
return true;
}
return true;
}
public boolean pause()
{
if (m_player != null && m_player.isPlaying())
{
m_player.pause();
return true;
}
return true;
}
public boolean isPending()
{
return m_pending;
}
public boolean isPrepared()
{
return m_prepared;
}
// MM-2012-10-15: [[ Bug 10437 ]] Patched MediaPlayer for use on Kindle Fire. It appears that the completion listerner
// is triggered to early on the Fire. Instead, invoke the listerner ourselves, using the duration of the audio clip.
// MM-2102-10-17: Updated to patch the isPlaying method to make sure it only returns false when the new completion callback is fired.
private class PatchedMediaPlayer extends MediaPlayer
{
private MediaPlayer.OnCompletionListener m_completion_listener;
private Runnable m_completion_callback;
private boolean m_playing;
public PatchedMediaPlayer()
{
super();
m_playing = false;
final MediaPlayer t_player = this;
m_completion_callback = new Runnable() {
public void run()
{
m_playing = false;
m_completion_listener.onCompletion(t_player);
}
};
}
public void start()
{
m_playing = true;
m_engine.getHandler().postDelayed(m_completion_callback, getDuration() - getCurrentPosition());
super.start();
}
public void pause()
{
m_playing = false;
m_engine.getHandler().removeCallbacks(m_completion_callback);
super.pause();
}
public void stop()
{
m_playing = false;
m_engine.getHandler().removeCallbacks(m_completion_callback);
super.stop();
}
public boolean isPlaying()
{
return m_playing;
}
public void setOnCompletionListener(MediaPlayer.OnCompletionListener p_listener)
{
m_completion_listener = p_listener;
}
}
}
}
}