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

Audio view model persistant playback position #23

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Voiceover>()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down Expand Up @@ -133,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 =
Expand All @@ -156,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()
}

/**
Expand Down
Loading