Merge: android/audio: fix multiple music playing when pausing app
authorJean Privat <jean@pryen.org>
Wed, 16 Sep 2015 17:11:50 +0000 (13:11 -0400)
committerJean Privat <jean@pryen.org>
Wed, 16 Sep 2015 17:11:50 +0000 (13:11 -0400)
Asked by @privat because friendz's app played multiple musics when pausing/resuming the app

Pull-Request: #1721
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

lib/android/audio.nit

index b361b4f..9cc54d7 100644 (file)
@@ -377,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")
@@ -482,6 +492,10 @@ class MediaPlayer
 end
 
 redef class PlayableAudio
+       # 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
 
@@ -544,11 +558,13 @@ redef class Sound
        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
@@ -606,11 +622,13 @@ redef class Music
        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
 
@@ -674,7 +692,16 @@ redef class App
 
        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
 
@@ -687,7 +714,10 @@ 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