Skip to content

Commit

Permalink
Fix oppia#5451 : java.lang.IllegalStateException - Media player has n…
Browse files Browse the repository at this point in the history
…ot been previously initialized (oppia#5475)

<!-- READ ME FIRST: Please fill in the explanation section below and
check off every point from the Essential Checklist! -->
## Explanation
Fixes oppia#5451 

The error occurs if the release is attempted before the MediaPlayer is
initialized or if releaseMediaPlayer() is called multiple times. To
address this, I have modified releaseMediaPlayer() to only release the
MediaPlayer if it has not already been released. These changes properly
handle the situation when releaseMediaPlayer() is called before
initialization.
  

## Essential Checklist
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [x] The PR title and explanation each start with "Fix #bugnum: " (If
this PR fixes part of an issue, prefix the title with "Fix part of
#bugnum: ...".)
- [ ] Any changes to
[scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets)
files have their rationale included in the PR explanation.
- [x] The PR follows the [style
guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide).
- [x] The PR does not contain any unnecessary code changes from Android
Studio
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)).
- [x] The PR is made from a branch that's **not** called "develop" and
is up-to-date with "develop".
- [x] The PR is **assigned** to the appropriate reviewers
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)).

## For UI-specific PRs only
<!-- Delete these section if this PR does not include UI-related
changes. -->
If your PR includes UI-related changes, then:
- Add screenshots for portrait/landscape for both a tablet & phone of
the before & after UI changes
- For the screenshots above, include both English and pseudo-localized
(RTL) screenshots (see [RTL
guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines))
- Add a video showing the full UX flow with a screen reader enabled (see
[accessibility
guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide))
- For PRs introducing new UI elements or color changes, both light and
dark mode screenshots must be included
- Add a screenshot demonstrating that you ran affected Espresso tests
locally & that they're passing

---------

Co-authored-by: Adhiambo Peres <59600948+adhiamboperes@users.noreply.github.com>
  • Loading branch information
subhajitxyz and adhiamboperes authored Aug 8, 2024
1 parent d89a377 commit bfa471e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,15 @@ class AudioPlayerController @Inject constructor(
*/
fun releaseMediaPlayer() {
audioLock.withLock {
check(mediaPlayerActive) { "Media player has not been previously initialized" }
mediaPlayerActive = false
isReleased = true
prepared = false
mediaPlayer.release()
stopUpdatingSeekBar()
playProgress = null
if (!isReleased) {
check(mediaPlayerActive) { "Media player has not been previously initialized" }
mediaPlayerActive = false
isReleased = true
prepared = false
mediaPlayer.release()
stopUpdatingSeekBar()
playProgress = null
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dagger.BindsInstance
import dagger.Component
import dagger.Module
import dagger.Provides
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -443,6 +444,17 @@ class AudioPlayerControllerTest {
.contains("Media player has not been previously initialized")
}

@Test
fun testController_releasePlayerMultipleTimes_doesNoThrowException() {
setUpMediaReadyApplication()
audioPlayerController.initializeMediaPlayer()

assertNoExceptionIsThrown {
audioPlayerController.releaseMediaPlayer()
audioPlayerController.releaseMediaPlayer()
}
}

@Test
fun testError_notPrepared_invokePlay_fails() {
setUpMediaReadyApplication()
Expand Down Expand Up @@ -872,6 +884,14 @@ class AudioPlayerControllerTest {
ApplicationProvider.getApplicationContext<TestApplication>().inject(this)
}

private fun assertNoExceptionIsThrown(block: () -> Unit) {
try {
block()
} catch (e: Exception) {
fail("Expected no exception, but got: $e")
}
}

// TODO(#89): Move this to a common test application component.
@Module
class TestModule {
Expand Down

0 comments on commit bfa471e

Please sign in to comment.