X-Git-Url: http://nitlanguage.org diff --git a/lib/android/audio.nit b/lib/android/audio.nit index c70dd40..9cc54d7 100644 --- a/lib/android/audio.nit +++ b/lib/android/audio.nit @@ -138,7 +138,6 @@ private extern class NativeMediaPlayer in "Java" `{ android.media.MediaPlayer `} self.setDataSource(fd, start_offset, length); return 1; }catch(Exception e) { - Log.e("Error loading the Media Player with a file descriptor", e.getMessage()); return 0; } `} @@ -378,6 +377,16 @@ class MediaPlayer # Load a sound for a given resource id fun load_sound(id: Int, context: NativeActivity): Music do + # FIXME: maybe find a better way to handle this situation + # If two different music are loaded with the same `MediaPlayer`, + # a new `NativeMediaPlayer` will be created for the secondd music + # and the nit program will loose the handle to the previous one + # If the previous music is playing, we need to stop it + if playing then + stop + reset + destroy + end self.nmedia_player = self.nmedia_player.create(context, id) if self.nmedia_player.is_java_null then self.error = new Error("Failed to load a sound") @@ -483,7 +492,11 @@ class MediaPlayer end redef class PlayableAudio - redef init do app.add_to_sounds(self) + # Flag to know if the user paused the sound + # Used when the app pause all sounds or resume all sounds + var paused: Bool = false + + redef init do add_to_sounds(self) end redef class Sound @@ -537,19 +550,21 @@ redef class Sound end redef fun play do - if self.error != null then return if not is_loaded then load + if self.error != null then return soundpool.play(soundpool_id) end redef fun pause do if self.error != null or not self.is_loaded then return soundpool.pause_stream(soundpool_id) + paused = true end redef fun resume do if self.error != null or not self.is_loaded then return soundpool.resume(soundpool_id) + paused = false end end @@ -599,27 +614,26 @@ redef class Music end redef fun play do - if self.error != null then return if not is_loaded then load + if self.error != null then return media_player.start end redef fun pause do if self.error != null or not self.is_loaded then return media_player.pause + paused = true end redef fun resume do if self.error != null or not self.is_loaded then return play + paused = false end end redef class App - # Sounds handled by the application, when you load a sound, it's added to this list. - # This array is used in `pause` and `resume` - private var sounds = new Array[PlayableAudio] # Returns the default MediaPlayer of the application. # When you load a music, it goes in this MediaPlayer. @@ -676,15 +690,18 @@ redef class App return add_to_sounds(default_mediaplayer.load_sound(resource_manager.raw_id(music), self.native_activity)).as(Music) end - # Factorizes `sounds.add` to use it in `load_music`, `load_sound`, `load_music_from_res` and `load_sound_from_res` - private fun add_to_sounds(sound: PlayableAudio): PlayableAudio do - sounds.add(sound) - return sound - end - redef fun on_pause do super - for s in sounds do s.pause + for s in sounds do + # Pausing sounds that are not already paused by user + # `s.paused` is set to false because `pause` set it to true + # and we want to know which sound has been paused by the user + # and which one has been paused by the app + if not s.paused then + s.pause + s.paused = false + end + end audio_manager.abandon_audio_focus end @@ -697,6 +714,22 @@ redef class App redef fun on_resume do super audio_manager.request_audio_focus - for s in sounds do s.resume + for s in sounds do + # Resumes only the sounds paused by the App + if not s.paused then s.resume + end + end +end + +redef class Sys + + # Sounds handled by the application, when you load a sound, it's added to this list. + # This array is used in `pause` and `resume` + private var sounds = new Array[PlayableAudio] + + # Factorizes `sounds.add` to use it in `load_music`, `load_sound`, `load_music_from_res` and `load_sound_from_res` + private fun add_to_sounds(sound: PlayableAudio): PlayableAudio do + sounds.add(sound) + return sound end end