Skip to content

Commit

Permalink
Fix media loading
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Nov 14, 2024
1 parent 3dde665 commit b8079d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.brahmkshatriya.echo.playback.MediaItemUtils.isLoaded
import dev.brahmkshatriya.echo.playback.MediaItemUtils.sourcesIndex
import dev.brahmkshatriya.echo.playback.MediaItemUtils.subtitleIndex
import dev.brahmkshatriya.echo.playback.MediaItemUtils.track
import dev.brahmkshatriya.echo.ui.exception.AppException.Companion.toAppException
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel.Companion.noClient
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel.Companion.trackNotSupported
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -53,7 +54,9 @@ class StreamableLoader(
val client = extension.instance.value.getOrNull()
if (client !is TrackClient)
throw Exception(context.trackNotSupported(extension.metadata.name).message)
return block(client)
return runCatching { block(client) }.getOrElse {
throw it.toAppException(extension)
}
}

private suspend fun loadTrack(item: MediaItem) = withClient(item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import dev.brahmkshatriya.echo.utils.saveToCache
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.io.IOException

@UnstableApi
class StreamableMediaSource(
Expand All @@ -63,7 +64,7 @@ class StreamableMediaSource(

private var error: Throwable? = null
override fun maybeThrowSourceInfoRefreshError() {
error?.let { throw it }
error?.let { throw IOException(it) }
super.maybeThrowSourceInfoRefreshError()
}

Expand All @@ -88,7 +89,11 @@ class StreamableMediaSource(

val sources = streamable.sources
actualSource = when (sources.size) {
0 -> throw Exception(context.getString(R.string.streamable_not_found))
0 -> {
error = Exception(context.getString(R.string.streamable_not_found))
return@launch
}

1 -> create(new, 0, sources.first())
else -> {
if (streamable.merged) MergingMediaSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ItemFragment : Fragment() {
})

fun concatAdapter(item: EchoMediaItem, itemsAdapter: ConcatAdapter): ConcatAdapter {
trackAdapter = TrackAdapter(clientId, view.transitionName, listener, item)
trackAdapter = TrackAdapter(clientId, view.transitionName, listener, item, true)
return when (item) {
is AlbumItem ->
ConcatAdapter(albumHeaderAdapter, trackAdapter, itemsAdapter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.Timeline
import androidx.media3.exoplayer.ExoPlayer
import dev.brahmkshatriya.echo.ui.exception.AppException
import dev.brahmkshatriya.echo.ui.exception.ExceptionFragment.Companion.toExceptionDetails
import dev.brahmkshatriya.echo.viewmodels.PlayerViewModel
import kotlinx.coroutines.launch
import java.io.IOException

class PlayerUiListener(
val player: Player,
Expand All @@ -36,7 +38,7 @@ class PlayerUiListener(

private fun updateNavigation() {
viewModel.nextEnabled.value = player.hasNextMediaItem()
viewModel.previousEnabled.value = player.currentMediaItemIndex >= 0
viewModel.previousEnabled.value = player.currentMediaItemIndex >= 0
}

private val delay = 500L
Expand All @@ -45,6 +47,7 @@ class PlayerUiListener(
private val handler = Handler(Looper.getMainLooper()).also {
it.post(updateProgressRunnable)
}

private fun updateProgress() {
viewModel.progress.value =
player.currentPosition.toInt() to player.bufferedPosition.toInt()
Expand Down Expand Up @@ -108,6 +111,12 @@ class PlayerUiListener(
}

override fun onPlayerError(error: PlaybackException) {
viewModel.createException(error.toExceptionDetails(viewModel.app))
var cause = error.cause ?: error
if (cause is IOException) {
cause = cause.cause ?: cause
}
if (cause is AppException) {
viewModel.apply { viewModelScope.launch { throwableFlow.emit(cause) } }
} else viewModel.createException(cause.toExceptionDetails(viewModel.app))
}
}

0 comments on commit b8079d1

Please sign in to comment.