From 2057e5f72226466309ae929c83f346b9bb20eb47 Mon Sep 17 00:00:00 2001 From: Rd Date: Wed, 18 Dec 2024 23:49:40 +0530 Subject: [PATCH 1/2] Resume player position when audio playback is toggled --- .../oppia/android/app/player/audio/AudioViewModel.kt | 6 ++++++ .../android/domain/audio/AudioPlayerController.kt | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt b/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt index e04b17f162a..48619fe9090 100644 --- a/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt +++ b/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt @@ -29,6 +29,8 @@ class AudioViewModel @Inject constructor( private val resourceHandler: AppLanguageResourceHandler ) : ObservableViewModel() { + var currentPos = 0 + private lateinit var state: State private lateinit var explorationId: String private var voiceoverMap = mapOf() @@ -195,7 +197,11 @@ class AudioViewModel @Inject constructor( is AsyncResult.Success -> when (playProgressResult.value.type) { PlayStatus.PREPARED -> { if (autoPlay) { + positionLiveData.observeForever { + if (it != 0) currentPos = it + } audioPlayerController.play(isPlayingFromAutoPlay = true, reloadingMainContent) + handleSeekTo(currentPos) } autoPlay = false reloadingMainContent = false diff --git a/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt b/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt index e54e3f17eac..3bd2e85120f 100644 --- a/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt +++ b/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt @@ -103,9 +103,13 @@ class AudioPlayerController @Inject constructor( } setMediaPlayerListeners() } - val progressLiveData = AudioMutableLiveData() - playProgress = progressLiveData - return progressLiveData + + if (playProgress == null) { + val progressLiveData = AudioMutableLiveData() + playProgress = progressLiveData + } + + return playProgress ?: AudioMutableLiveData() } /** From a47bb3de0620f2a6384caa5c0c80a21c9b79b051 Mon Sep 17 00:00:00 2001 From: Rd Date: Thu, 19 Dec 2024 01:04:23 +0530 Subject: [PATCH 2/2] Retaining with configuration changes --- .../android/domain/audio/AudioPlayerController.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt b/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt index 3bd2e85120f..1133070347e 100644 --- a/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt +++ b/domain/src/main/java/org/oppia/android/domain/audio/AudioPlayerController.kt @@ -137,8 +137,17 @@ class AudioPlayerController @Inject constructor( mediaPlayer.setOnPreparedListener { prepared = true duration = it.duration - playProgress?.value = - AsyncResult.Success(PlayProgress(PlayStatus.PREPARED, 0, duration)) + val prevPos = playProgress?.value + if (prevPos is AsyncResult.Success) { + val prevPosProg = prevPos.value + val prevPosition = prevPosProg.position + + playProgress?.value = + AsyncResult.Success(PlayProgress(PlayStatus.PREPARED, prevPosition, duration)) + } else { + playProgress?.value = + AsyncResult.Success(PlayProgress(PlayStatus.PREPARED, 0, duration)) + } } mediaPlayer.setOnErrorListener { _, what, extra -> playProgress?.value = @@ -160,7 +169,7 @@ class AudioPlayerController @Inject constructor( exceptionsController.logNonFatalException(e) oppiaLogger.e("AudioPlayerController", "Failed to set data source for media player", e) } - playProgress?.value = AsyncResult.Pending() + if (playProgress == null) playProgress?.value = AsyncResult.Pending() } /**