From 4e7b757cb86d867b9177c2f2ba12e6acfa149ede Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Tue, 14 Feb 2023 12:36:43 +0100 Subject: [PATCH 1/2] avoid NPE for voice message playback Exception java.lang.NullPointerException: at com.nextcloud.talk.controllers.ChatController.startPlayback (ChatController.kt:985) at com.nextcloud.talk.controllers.ChatController.access$startPlayback (ChatController.kt:219) at com.nextcloud.talk.controllers.ChatController$downloadFileToCache$1.invoke (ChatController.kt:1130) at com.nextcloud.talk.controllers.ChatController$downloadFileToCache$1.invoke (ChatController.kt:1128) at com.nextcloud.talk.controllers.ChatController.downloadFileToCache$lambda$19 (ChatController.kt:1128) at com.nextcloud.talk.controllers.ChatController.$r8$lambda$oLS0bGL_zLma74VfkfStO_mReb4 at com.nextcloud.talk.controllers.ChatController$$ExternalSyntheticLambda10.onChanged at androidx.lifecycle.LiveData.considerNotify (LiveData.java:133) at androidx.lifecycle.LiveData.dispatchingValue (LiveData.java:151) at androidx.lifecycle.LiveData.setValue (LiveData.java:309) at androidx.lifecycle.MutableLiveData.setValue (MutableLiveData.java:50) at androidx.lifecycle.LiveData$1.run (LiveData.java:93) at android.os.Handler.handleCallback (Handler.java:942) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loopOnce (Looper.java:226) at android.os.Looper.loop (Looper.java:313) at android.app.ActivityThread.main (ActivityThread.java:8775) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067) Signed-off-by: Marcel Hibbe --- .../talk/controllers/ChatController.kt | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index f1c22109d7..11b696fd30 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -982,25 +982,27 @@ class ChatController(args: Bundle) : initMediaPlayer(message) - if (!mediaPlayer!!.isPlaying) { - mediaPlayer!!.start() - } - - mediaPlayerHandler = Handler() - activity?.runOnUiThread(object : Runnable { - override fun run() { - if (mediaPlayer != null) { - val currentPosition: Int = mediaPlayer!!.currentPosition / VOICE_MESSAGE_SEEKBAR_BASE - message.voiceMessagePlayedSeconds = currentPosition - adapter?.update(message) + mediaPlayer?.let { + if (!it.isPlaying) { + it.start() + } + + mediaPlayerHandler = Handler() + activity?.runOnUiThread(object : Runnable { + override fun run() { + if (mediaPlayer != null) { + val currentPosition: Int = mediaPlayer!!.currentPosition / VOICE_MESSAGE_SEEKBAR_BASE + message.voiceMessagePlayedSeconds = currentPosition + adapter?.update(message) + } + mediaPlayerHandler.postDelayed(this, SECOND) } - mediaPlayerHandler.postDelayed(this, SECOND) - } - }) + }) - message.isDownloadingVoiceMessage = false - message.isPlayingVoiceMessage = true - adapter?.update(message) + message.isDownloadingVoiceMessage = false + message.isPlayingVoiceMessage = true + adapter?.update(message) + } } private fun pausePlayback(message: ChatMessage) { From 291f527b880a9660dc5db9ddd2926eebb3df8415 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Tue, 14 Feb 2023 12:57:41 +0100 Subject: [PATCH 2/2] Remove error logging for voice message that was not correct media player can already exist here when playback was paused, that's okay. Signed-off-by: Marcel Hibbe --- .../main/java/com/nextcloud/talk/controllers/ChatController.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 11b696fd30..c13c412bf8 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -1040,8 +1040,6 @@ class ChatController(args: Bundle) : Log.e(TAG, "failed to initialize mediaPlayer", e) Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show() } - } else { - Log.e(TAG, "mediaPlayer was not null. This should not happen!") } }