audio: move some services between the abstraction and the Android layers
[nit.git] / lib / android / audio.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Android audio services, wraps a part of android audio API
18 # This module modifies the default behaviour of the audio loading:
19 # It is first loaded from the `res/raw` folder.
20 # The file extension is not needed for the `res/raw` loading part.
21 # If it didn't work, it is loaded from the `assets` folder.
22 # The file extension is needed for the `assets` loading part.
23 #
24 # `assets` contains the portable version of sounds, since the `res` folder exsists only in android projects.
25 #
26 # For this example, the sounds "test_sound" and "test_music" are located in the "assets/sounds" folder,
27 # they both have ".ogg" extension. "test_sound" is a short sound and "test_music" a music track
28 #
29 # ~~~nitish
30 # # Note that you need to specify the path from "assets" folder and the extension
31 # var s = app.load_sound("sounds/test_sound.ogg")
32 # var m = app.load_music("sounds/test_music.ogg")
33 # s.play
34 # m.play
35 # ~~~
36 #
37 # Now, the sounds are in "res/raw"
38 # ~~~nitish
39 # s = app.load_sound_from_res("test_sound")
40 # m = app.load_music_from_res("test_sound")
41 # s.play
42 # m.play
43 # ~~~
44 #
45 # See http://developer.android.com/reference/android/media/package-summary.html for more infos
46 module audio
47
48 import java
49 import java::io
50 intrude import assets_and_resources
51 import activities
52 import app::audio
53
54 in "Java" `{
55 import android.media.MediaPlayer;
56 import android.media.SoundPool;
57 import java.io.IOException;
58 import android.media.AudioManager;
59 import android.media.AudioManager.OnAudioFocusChangeListener;
60 import android.content.Context;
61 import android.util.Log;
62 `}
63
64 # FIXME: This listener is not working at the moment, but is needed to gain or give up the audio focus
65 # of the application
66 in "Java inner" `{
67 static OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
68 public void onAudioFocusChange(int focusChange) {
69 if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
70 }else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
71 }else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
72 }
73 }
74 };
75 `}
76
77 # AudioManager of the application, used to manage the audio mode
78 private extern class NativeAudioManager in "Java" `{ android.media.AudioManager `}
79 super JavaObject
80
81 # Current audio mode.
82 # ( MODE_NORMAL = 0, MODE_RINGTONE = 1, MODE_IN_CALL = 2 or MODE_IN_COMMUNICATION = 3 )
83 fun mode: Int in "Java" `{ return self.getMode(); `}
84
85 # Sets the audio mode.
86 # ( MODE_NORMAL = 0, MODE_RINGTONE = 1, MODE_IN_CALL = 2 or MODE_IN_COMMUNICATION = 3 )
87 fun mode=(i: Int) in "Java" `{ self.setMode((int)i); `}
88
89 # Sends a request to obtain audio focus
90 fun request_audio_focus: Int in "Java" `{
91 return self.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
92 `}
93
94 # Gives up audio focus
95 fun abandon_audio_focus: Int in "Java" `{ return self.abandonAudioFocus(afChangeListener); `}
96 end
97
98 # Media Player from Java, used to play long sounds or musics, not simultaneously
99 # This is a low-level class, use `MediaPlater` instead
100 private extern class NativeMediaPlayer in "Java" `{ android.media.MediaPlayer `}
101 super JavaObject
102
103 new in "Java" `{
104 MediaPlayer mp = new MediaPlayer();
105 mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
106 return mp;
107 `}
108 fun start in "Java" `{ self.start(); `}
109 fun prepare in "Java" `{
110 try {
111 self.prepare();
112 }catch(Exception e) {
113 Log.e("Error preparing the Media Player", e.getMessage());
114 e.printStackTrace();
115 }
116 `}
117
118 new create(context: NativeActivity, id: Int): NativeMediaPlayer
119 in "Java" `{
120 try {
121 return MediaPlayer.create(context, (int)id);
122 }catch(Exception e) {
123 return null;
124 }
125 `}
126
127 fun pause in "Java" `{ self.pause(); `}
128 fun stop in "Java" `{ self.stop(); `}
129 fun playing: Bool in "Java" `{ return self.isPlaying(); `}
130 fun release in "Java" `{ self.release(); `}
131 fun duration: Int in "Java" `{ return self.getDuration(); `}
132 fun looping: Bool in "Java" `{ return self.isLooping(); `}
133 fun looping=(b: Bool) in "Java" `{ self.setLooping(b); `}
134 fun volume=(vol: Float) in "Java" `{ self.setVolume((float)vol, (float)vol); `}
135 fun both_volume(left_volume, right_volume: Float) in "Java" `{ self.setVolume((float)left_volume, (float)right_volume); `}
136 fun stream_type=(stream_type: Int) in "Java" `{ self.setAudioStreamType((int)stream_type); `}
137 fun data_source_fd(fd: NativeFileDescriptor, start_offset, length: Int): Int in "Java" `{
138 try {
139 self.setDataSource(fd, start_offset, length);
140 return 1;
141 }catch(Exception e) {
142 return 0;
143 }
144 `}
145 fun data_source_path(path: JavaString): Int in "Java" `{
146 try {
147 self.setDataSource(path);
148 return 1;
149 }catch(Exception e) {
150 Log.e("Error loading the Media Player", e.getMessage());
151 return 0;
152 }
153 `}
154 fun reset in "Java" `{ self.reset(); `}
155 end
156
157 # Sound Pool from Java, used to play sounds simultaneously
158 # This is a low-level class, use `SoundPool`instead
159 private extern class NativeSoundPool in "Java" `{ android.media.SoundPool `}
160 super JavaObject
161
162 new(max_streams, stream_type, src_quality: Int) in "Java" `{
163 return new SoundPool((int)max_streams, (int)stream_type, (int)src_quality);
164 `}
165 fun load_asset_fd(afd: NativeAssetFileDescriptor, priority: Int): Int in "Java" `{
166 try {
167 int id = self.load(afd, (int)priority);
168 return id;
169 }catch(Exception e){
170 return -1;
171 }
172 `}
173
174 fun load_id(context: NativeActivity, resid, priority: Int): Int in "Java" `{
175 try {
176 int id = self.load(context, (int)resid, (int)priority);
177 return id;
178 }catch(Exception e){
179 return -1;
180 }
181 `}
182
183 fun load_path(path: JavaString, priority: Int): Int in "Java" `{
184 try {
185 int id = self.load(path, (int)priority);
186 return id;
187 }catch(Exception e){
188 return -1;
189 }
190 `}
191
192 fun play(sound_id: Int, left_volume, right_volume: Float, priority, l: Int, rate: Float): Int in "Java" `{
193 return self.play((int)sound_id, (float)left_volume, (float)right_volume, (int)priority, (int)l, (float)rate);
194 `}
195 fun pause(stream_id: Int) in "Java" `{ self.pause((int)stream_id); `}
196 fun auto_pause in "Java" `{ self.autoPause(); `}
197 fun auto_resume in "Java" `{ self.autoResume(); `}
198 fun resume(stream_id: Int) in "Java" `{ self.resume((int)stream_id); `}
199 fun set_loop(stream_id, l: Int) in "Java" `{ self.setLoop((int)stream_id, (int)l); `}
200 fun set_priority(stream_id, priority: Int) in "Java" `{ self.setPriority((int)stream_id, (int)priority); `}
201 fun set_rate(stream_id: Int, rate: Float) in "Java" `{ self.setRate((int)stream_id, (float)rate); `}
202 fun set_volume(stream_id: Int, left_volume, right_volume: Float) in "Java" `{ self.setVolume((int)stream_id, (float)left_volume, (float)right_volume); `}
203 fun stop(stream_id: Int) in "Java" `{ self.stop((int)stream_id); `}
204 fun unload(sound_id: Int): Bool in "Java" `{ return self.unload((int)sound_id); `}
205 fun release in "Java" `{ self.release(); `}
206 end
207
208
209 # Used to play sound, best suited for sounds effects in apps or games
210 class SoundPool
211
212 # Latest error on this sound pool
213 var error: nullable Error = null
214
215 private var nsoundpool: NativeSoundPool is noinit
216
217 # The maximum number of simultaneous streams for this SoundPool
218 var max_streams = 10 is writable
219
220 # The audio stream type, 3 is STREAM_MUSIC, default for game application
221 var stream_type = 3 is writable
222
223 # The sample-rate converter quality, currently has no effect
224 var src_quality = 0 is writable
225
226 # Left volume value, range 0.0 to 1.0
227 var left_volume = 1.0 is writable
228
229 # Right volume value, range 0.0 to 1.0
230 var right_volume = 1.0 is writable
231
232 # Playback rate, 1.0 = normal playback, range 0.5 to 2.0
233 var rate = 1.0 is writable
234
235 # Loop mode, 0 = no loop, -1 = loop forever
236 var looping = 0 is writable
237
238 # Stream priority
239 private var priority = 1
240
241 init do self.nsoundpool = new NativeSoundPool(max_streams, stream_type, src_quality)
242
243 # Load the sound from an asset file descriptor
244 # this function is for advanced use
245 private fun load_asset_fd(afd: NativeAssetFileDescriptor): Sound do
246 var resval = nsoundpool.load_asset_fd(afd, self.priority)
247 if resval == -1 then
248 self.error = new Error("Unable to load sound from assets")
249 return new Sound.priv_init(null, -1, self, self.error)
250 else
251 return new Sound.priv_init(null, resval, self, null)
252 end
253 end
254
255 # Returns only the id corresponding to the soundpool where the sound is loaded.
256 # Needed by `load` of `Sound`.
257 private fun load_asset_fd_rid(afd: NativeAssetFileDescriptor): Int do
258 return nsoundpool.load_asset_fd(afd, self.priority)
259 end
260
261 # Load the sound from its resource id
262 fun load_id(context: NativeActivity, id:Int): Sound do
263 var resval = nsoundpool.load_id(context, id, priority)
264 if resval == -1 then
265 self.error = new Error("Unable to load sound from assets")
266 return new Sound.priv_init(null, -1, self, self.error)
267 else
268 return new Sound.priv_init(null, resval, self, null)
269 end
270 end
271
272 # Returns only the id corresponding to the soundpool where the sound is loaded.
273 private fun load_id_rid(context: NativeActivity, id: Int): Int do
274 return nsoundpool.load_id(context, id, priority)
275 end
276
277 # Load the sound from the specified path
278 fun load_path(path: String): Sound do
279 sys.jni_env.push_local_frame(1)
280 var resval = nsoundpool.load_path(path.to_java_string, priority)
281 sys.jni_env.pop_local_frame
282 if resval == -1 then
283 self.error = new Error("Unable to load sound from path: " + path)
284 return new Sound.priv_init(null, -1, self, self.error)
285 else
286 return new Sound.priv_init(null, resval, self, null)
287 end
288 end
289
290 # Play a sound from a sound ID
291 # return non-zero streamID if successful, zero if failed
292 fun play(id: Int): Int do
293 return nsoundpool.play(id, left_volume, right_volume, priority, looping, rate)
294 end
295
296 # Load a sound by its `name` in the resources, the sound must be in the `res/raw` folder
297 fun load_name(resource_manager: ResourcesManager, context: NativeActivity, name: String): Sound do
298 var id = resource_manager.raw_id(name)
299 var resval = nsoundpool.load_id(context, id, priority)
300 if resval == -1 then
301 self.error = new Error("Unable to load sound from resources: " + name)
302 return new Sound.priv_init(null, -1, self, self.error)
303 else
304 return new Sound.priv_init(null, resval, self, null)
305 end
306 end
307
308 # Returns only the id corresponding to the soundpool where the sound is loaded.
309 private fun load_name_rid(resource_manager: ResourcesManager, context: NativeActivity, sound: String): Int do
310 var id = resource_manager.raw_id(sound)
311 return nsoundpool.load_id(context, id, priority)
312 end
313
314 # Pause a playback stream
315 fun pause_stream(stream_id: Int) do nsoundpool.pause(stream_id)
316
317 # Pause all active_streams
318 fun auto_pause do nsoundpool.auto_pause
319
320 # Resume all previously active streams
321 fun auto_resume do nsoundpool.auto_resume
322
323 # Resume a playback stream
324 fun resume(stream_id: Int) do nsoundpool.resume(stream_id)
325
326 # Set loop mode on a stream
327 fun stream_loop=(stream_id, looping: Int) do nsoundpool.set_loop(stream_id, looping)
328
329 # Change stream priority
330 fun stream_priority=(stream_id, priority: Int) do nsoundpool.set_priority(stream_id, priority)
331
332 # Change playback rate
333 fun stream_rate=(stream_id: Int, rate: Float) do nsoundpool.set_rate(stream_id, rate)
334
335 # Set stream volume
336 fun stream_volume(stream_id: Int, left_volume, right_volume: Float) do
337 nsoundpool.set_volume(stream_id, left_volume, right_volume)
338 end
339
340 # Stop a playback stream
341 fun stop_stream(stream_id: Int) do nsoundpool.stop(stream_id)
342
343 # Unload a sound from a sound ID
344 fun unload(sound: Sound): Bool do return nsoundpool.unload(sound.soundpool_id)
345
346 # Destroys the object
347 fun destroy do nsoundpool.release
348 end
349
350 # Used to play sounds, designed to use with medium sized sounds or streams
351 # The Android MediaPlayer has a complex state diagram that you'll need to
352 # respect if you want your MediaPlayer to work fine, see the android doc
353 class MediaPlayer
354 private var nmedia_player: NativeMediaPlayer is noinit
355
356 # Used to control the state of the mediaplayer
357 private var is_prepared = false is writable
358
359 # The sound associated with this mediaplayer
360 var sound: nullable Music = null is writable
361
362 # Error gestion
363 var error: nullable Error = null
364
365 # Create a new MediaPlayer, but no sound is attached, you'll need
366 # to use `load_sound` before using it
367 init do self.nmedia_player = new NativeMediaPlayer
368
369 # Init the mediaplayer with a sound resource id
370 init from_id(context: NativeActivity, id: Int) do
371 self.nmedia_player = new NativeMediaPlayer.create(context, id)
372 if self.nmedia_player.is_java_null then
373 self.error = new Error("Failed to create the MediaPlayer")
374 self.sound = new Music.priv_init(id, self, self.error)
375 end
376 self.sound = new Music.priv_init(id, self, null)
377 end
378
379 # Load a sound for a given resource id
380 fun load_sound(id: Int, context: NativeActivity): Music do
381 # FIXME: maybe find a better way to handle this situation
382 # If two different music are loaded with the same `MediaPlayer`,
383 # a new `NativeMediaPlayer` will be created for the secondd music
384 # and the nit program will loose the handle to the previous one
385 # If the previous music is playing, we need to stop it
386 if playing then
387 stop
388 reset
389 destroy
390 end
391
392 self.nmedia_player = new NativeMediaPlayer.create(context, id)
393 if self.nmedia_player.is_java_null then
394 self.error = new Error("Failed to load a sound")
395 self.sound = new Music.priv_init(id, self, new Error("Sound loading failed"))
396 return self.sound.as(not null)
397 else
398 if self.error != null then self.error = null
399 self.sound = new Music.priv_init(id, self, null)
400 self.is_prepared = true
401 return self.sound.as(not null)
402 end
403 end
404
405 # Starts or resumes playback
406 # REQUIRE `self.sound != null`
407 fun start do
408 if self.error != null then return
409 if not is_prepared then prepare
410 nmedia_player.start
411 end
412
413 # Stops playback after playback has been stopped or paused
414 # REQUIRE `self.sound != null`
415 fun stop do
416 if self.error != null then return
417 is_prepared = false
418 nmedia_player.stop
419 end
420
421 # Prepares the player for playback, synchronously
422 # REQUIRE `self.sound != null`
423 fun prepare do
424 if self.error != null then return
425 assert sound != null
426 nmedia_player.prepare
427 is_prepared = true
428 end
429
430 # Pauses playback
431 # REQUIRE `self.sound != null`
432 fun pause do
433 if self.error != null then return
434 assert sound != null
435 nmedia_player.pause
436 end
437
438 # Checks whether the mediaplayer is playing
439 fun playing: Bool do return nmedia_player.playing
440
441 # Releases the resources associated with this MediaPlayer
442 fun destroy do
443 nmedia_player.release
444 self.sound = null
445 end
446
447 # Reset MediaPlayer to its initial state
448 fun reset do nmedia_player.reset
449
450 # Sets the datasource (file-path or http/rtsp URL) to use
451 fun data_source(path: String): Music do
452 sys.jni_env.push_local_frame(1)
453 var retval = nmedia_player.data_source_path(path.to_java_string)
454 sys.jni_env.pop_local_frame
455 if retval == 0 then
456 self.error = new Error("could not load the sound " + path)
457 self.sound = new Music.priv_init(null, self, self.error)
458
459 else
460 self.sound = new Music.priv_init(null, self, null)
461 end
462 return self.sound.as(not null)
463 end
464 # Sets the data source (NativeFileDescriptor) to use
465 fun data_source_fd(fd: NativeAssetFileDescriptor): Music do
466 if not fd.is_java_null then
467 if nmedia_player.data_source_fd(fd.file_descriptor, fd.start_offset, fd.length) == 0 then
468 self.error = new Error("could not load the sound")
469 self.sound = new Music.priv_init(null, self, self.error)
470 else
471 self.sound = new Music.priv_init(null, self, null)
472 end
473 return self.sound.as(not null)
474 else
475 var error = new Error("could not load the sound")
476 return new Music.priv_init(null, self, error)
477 end
478 end
479
480 # Checks whether the MediaPlayer is looping or non-looping
481 fun looping: Bool do return nmedia_player.looping
482
483 # Sets the player to be looping or non-looping
484 fun looping=(b: Bool) do nmedia_player.looping = b
485
486 # Sets the volume on this player
487 fun volume=(volume: Float) do nmedia_player.volume = volume
488
489 # Sets the left volume and the right volume of this player
490 fun both_volume(left_volume, right_volume: Float) do nmedia_player.both_volume(left_volume, right_volume)
491
492 # Sets the audio stream type for this media player
493 fun stream_type=(stream_type: Int) do nmedia_player.stream_type = stream_type
494 end
495
496 redef class PlayableAudio
497 # Flag to know if the user paused the sound
498 # Used when the app pause all sounds or resume all sounds
499 var paused: Bool = false
500
501 # Is `self` already loaded?
502 protected var is_loaded = false is writable
503
504 redef var error = null
505 end
506
507 redef class Sound
508
509 # Resource ID of this sound
510 var id: nullable Int is noinit
511
512 # The SoundPool who loaded this sound
513 var soundpool: SoundPool is noinit
514
515 # The SoundID of this sound in his SoundPool
516 var soundpool_id: Int is noinit
517
518 private init priv_init(id: nullable Int, soundpool_id: Int, soundpool: SoundPool, error: nullable Error) is nosuper do
519 self.id = id
520 self.soundpool_id = soundpool_id
521 self.soundpool = soundpool
522 if error != null then
523 self.error = error
524 else
525 self.is_loaded = true
526 end
527 end
528
529 redef fun load do
530 if is_loaded then return
531 var retval_resources = app.default_soundpool.load_name_rid(app.resource_manager, app.native_activity, path.strip_extension)
532 if retval_resources == -1 then
533 self.error = new Error("Failed to load " + path)
534 var nam = app.asset_manager.open_fd(path)
535 if nam.is_java_null then
536 self.error = new Error("Failed to get file descriptor for " + path)
537 else
538 var retval_assets = app.default_soundpool.load_asset_fd_rid(nam)
539 if retval_assets == -1 then
540 self.error = new Error("Failed to load " + path)
541 else
542 self.soundpool_id = retval_assets
543 self.soundpool = app.default_soundpool
544 self.error = null
545 self.soundpool.error = null
546 end
547 end
548 else
549 self.soundpool_id = retval_resources
550 self.soundpool = app.default_soundpool
551 self.error = null
552 self.soundpool.error = null
553 end
554 is_loaded = true
555
556 var error = error
557 if error != null then print_error error
558 end
559
560 redef fun play do
561 if not is_loaded then load
562 if self.error != null then return
563 soundpool.play(soundpool_id)
564 end
565
566 redef fun pause do
567 if self.error != null or not self.is_loaded then return
568 soundpool.pause_stream(soundpool_id)
569 paused = true
570 end
571
572 redef fun resume do
573 if self.error != null or not self.is_loaded then return
574 soundpool.resume(soundpool_id)
575 paused = false
576 end
577
578 end
579
580 redef class Music
581
582 # Resource ID of this sound
583 var id: nullable Int is noinit
584
585 # The MediaPlayer who loaded this sound
586 var media_player: MediaPlayer is noinit
587
588 private init priv_init(id: nullable Int, media_player: MediaPlayer, error: nullable Error) is nosuper do
589 self.id = id
590 self.media_player = media_player
591 if error != null then
592 self.error = error
593 else
594 self.is_loaded = true
595 end
596 end
597
598 redef fun load do
599 if is_loaded then return
600 var mp_sound_resources = app.default_mediaplayer.load_sound(app.resource_manager.raw_id(path.strip_extension), app.native_activity)
601 if mp_sound_resources.error != null then
602 self.error = mp_sound_resources.error
603 var nam = app.asset_manager.open_fd(path)
604 if nam.is_java_null then
605 self.error = new Error("Failed to get file descriptor for " + path)
606 else
607 var mp_sound_assets = app.default_mediaplayer.data_source_fd(nam)
608 if mp_sound_assets.error != null then
609 self.error = mp_sound_assets.error
610 else
611 self.media_player = app.default_mediaplayer
612 self.error = null
613 self.media_player.error = null
614 end
615 end
616 else
617 self.media_player = app.default_mediaplayer
618 self.error = null
619 self.media_player.error = null
620 end
621 is_loaded = true
622
623 var error = error
624 if error != null then print_error error
625 end
626
627 redef fun play do
628 if not is_loaded then load
629 if self.error != null then return
630 media_player.start
631 end
632
633 redef fun pause do
634 if self.error != null or not self.is_loaded then return
635 media_player.pause
636 paused = true
637 end
638
639 redef fun resume do
640 if self.error != null or not self.is_loaded then return
641 play
642 paused = false
643 end
644 end
645
646 redef class App
647
648 # Returns the default MediaPlayer of the application.
649 # When you load a music, it goes in this MediaPlayer.
650 # Use it for advanced sound management
651 var default_mediaplayer: MediaPlayer is lazy do return new MediaPlayer
652
653 # Returns the default MediaPlayer of the application.
654 # When you load a short sound (not a music), it's added to this soundpool.
655 # Use it for advanced sound management.
656 var default_soundpool: SoundPool is lazy do return new SoundPool
657
658 # Get the native audio manager
659 private fun audio_manager: NativeAudioManager import native_activity in "Java" `{
660 return (AudioManager)App_native_activity(self).getSystemService(Context.AUDIO_SERVICE);
661 `}
662
663 # Sets the stream of the app to STREAM_MUSIC.
664 # STREAM_MUSIC is the default stream used by android apps.
665 private fun manage_audio_stream import native_activity in "Java" `{
666 App_native_activity(self).setVolumeControlStream(AudioManager.STREAM_MUSIC);
667 `}
668
669 # Same as `load_sound` but load the sound from the `res/raw` folder
670 fun load_sound_from_res(sound_name: String): Sound do
671 return default_soundpool.load_name(resource_manager, native_activity, sound_name)
672 end
673
674 # Same as `load_music` but load the sound from the `res/raw` folder
675 fun load_music_from_res(music: String): Music do
676 return default_mediaplayer.load_sound(resource_manager.raw_id(music), native_activity)
677 end
678
679 redef fun on_pause do
680 super
681 for s in sounds do
682 # Pausing sounds that are not already paused by user
683 # `s.paused` is set to false because `pause` set it to true
684 # and we want to know which sound has been paused by the user
685 # and which one has been paused by the app
686 if not s.paused then
687 s.pause
688 s.paused = false
689 end
690 end
691 audio_manager.abandon_audio_focus
692 end
693
694 redef fun on_create do
695 super
696 audio_manager.request_audio_focus
697 manage_audio_stream
698 end
699
700 redef fun on_resume do
701 super
702 audio_manager.request_audio_focus
703 for s in sounds do
704 # Resumes only the sounds paused by the App
705 if not s.paused then s.resume
706 end
707 end
708 end