Skip to content

Commit

Permalink
Fixed the app player
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jul 13, 2024
1 parent a215b6e commit 5bb704d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.material3.SliderDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -94,11 +95,15 @@ fun LessonContent(
viewModel: LessonsViewModel
) {
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.anim_plant))

val context = LocalContext.current
val sliderPosition by viewModel.playbackPosition.collectAsState()
val playbackDuration by viewModel.playbackDuration.collectAsState()
val isPlaying by viewModel.isPlaying.collectAsState()

LaunchedEffect(key1 = lessonDetails) {
viewModel.preparePlayer(context, lessonDetails.lessonDetails.audioResId)
}

Column(
modifier = modifier
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import com.d4rk.englishwithlidia.plus.data.model.ui.lessons.UiLessonsAsset
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -41,41 +43,41 @@ class LessonsViewModel @OptIn(UnstableApi::class)

private var player: Player? = null

fun preparePlayer(context: Context, audioPath: String) {
init {
viewModelScope.launch {
player?.release()

val resourceId = context.resources.getIdentifier(
audioPath, "raw", context.packageName
)
player = ExoPlayer.Builder(getApplication()).build()
}
}

if (resourceId != 0) {
val audioUri = Uri.parse("android.resource://${context.packageName}/$resourceId")
player = ExoPlayer.Builder(getApplication()).build().apply {
setMediaItem(MediaItem.fromUri(audioUri))
prepare()
playWhenReady = false
fun preparePlayer(context: Context, audioResId: Int) {
viewModelScope.launch {
player?.release()

addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
_isPlaying.value = isPlaying
}
val audioUri = Uri.parse("android.resource://${context.packageName}/$audioResId")
player = ExoPlayer.Builder(getApplication()).build().apply {
setMediaItem(MediaItem.fromUri(audioUri))
prepare()
playWhenReady = false

override fun onPlaybackStateChanged(playbackState: Int) {
if (playbackState == Player.STATE_READY) {
_playbackDuration.value = duration
}
}
addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
_isPlaying.value = isPlaying
}

override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo,
reason: Int
) {
_playbackPosition.value = currentPosition
override fun onPlaybackStateChanged(playbackState: Int) {
if (playbackState == Player.STATE_READY) {
_playbackDuration.value = duration
}
})
}
}

override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo,
reason: Int
) {
_playbackPosition.value = currentPosition
}
})
}
startPositionUpdateJob()
}
Expand All @@ -99,6 +101,10 @@ class LessonsViewModel @OptIn(UnstableApi::class)
viewModelScope.launch {
while (true) {
_playbackPosition.value = player?.currentPosition ?: 0L
delay(100)
if (player?.isPlaying == false) {
break
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<string name="license">License</string>

<string name="about">About</string>
<string name="summary_preference_settings_about">Learn more about Cart Calculator</string>
<string name="summary_preference_settings_about">Learn more about English with Lidia</string>

<string name="app_info">App info</string>
<string name="app_build_version">Application build version</string>
Expand Down

0 comments on commit 5bb704d

Please sign in to comment.