Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5451 : java.lang.IllegalStateException - Media player has not been previously initialized #5475

Merged
Merged
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
Loading