Browse Source

avoid IllegalStateException for ChatController#stopMediaPlayer

didn't see this crash here, but it might have happened similar to b29fccac where it happened.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 2 years ago
parent
commit
be5a60dc77
1 changed files with 13 additions and 3 deletions
  1. 13 3
      app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

+ 13 - 3
app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

@@ -1036,9 +1036,18 @@ class ChatController(args: Bundle) :
 
         mediaPlayerHandler.removeCallbacksAndMessages(null)
 
-        mediaPlayer?.stop()
-        mediaPlayer?.release()
-        mediaPlayer = null
+        try {
+            mediaPlayer?.let {
+                if (it.isPlaying) {
+                    it.stop()
+                }
+            }
+        } catch (e: IllegalStateException) {
+            Log.e(TAG, "mediaPlayer was not initialized", e)
+        } finally {
+            mediaPlayer?.release()
+            mediaPlayer = null
+        }
     }
 
     override fun updateMediaPlayerProgressBySlider(messageWithSlidedProgress: ChatMessage, progress: Int) {
@@ -3426,6 +3435,7 @@ class ChatController(args: Bundle) :
         private const val VOICE_RECORD_CANCEL_SLIDER_X: Int = -50
         private const val VOICE_MESSAGE_META_DATA = "{\"messageType\":\"voice-message\"}"
         private const val VOICE_MESSAGE_FILE_SUFFIX = ".mp3"
+
         // Samplingrate 22050 was chosen because somehow 44100 failed to playback on safari when recorded on android.
         // Please test with firefox, chrome, safari and mobile clients if changing anything regarding the sound.
         private const val VOICE_MESSAGE_SAMPLING_RATE = 22050