Skip to content

Commit

Permalink
fix cast button
Browse files Browse the repository at this point in the history
  • Loading branch information
Dark25 committed Jan 5, 2025
1 parent 18eed61 commit 0151c66
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
61 changes: 33 additions & 28 deletions app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ class PlayerActivity : BaseActivity() {
)
}
}

isCastApiAvailable =
GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS
try {
Expand Down Expand Up @@ -1367,7 +1366,7 @@ class PlayerActivity : BaseActivity() {
}
// <-- AM (DISCORD)

// -- CAST --
// -- CAST --

private fun setupCastListener() {
mSessionManagerListener = object : SessionManagerListener<CastSession> {
Expand Down Expand Up @@ -1415,37 +1414,43 @@ class PlayerActivity : BaseActivity() {
}

private fun loadRemoteMedia() {
if (mCastSession == null) {
// Validar que las dependencias necesarias estén inicializadas
if (mCastSession == null || viewModel.currentAnime.value == null || viewModel.currentEpisode.value == null) {
logcat(LogPriority.ERROR) { "Cannot load remote media: Missing session or data" }
return
}
val remoteMediaClient = mCastSession!!.remoteMediaClient ?: return
remoteMediaClient.load(
MediaLoadRequestData.Builder()
.setMediaInfo(buildMediaInfo())
.setAutoplay(true)
.setCurrentTime(player.timePos!!.toLong() * 1000).build(),
)
try {
remoteMediaClient.load(
MediaLoadRequestData.Builder()
.setMediaInfo(buildMediaInfo())
.setAutoplay(true)
.setCurrentTime((player.timePos ?: 0).toLong() * 1000)
.build(),
)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e) { "Error loading remote media" }
}
}

val title = viewModel.currentAnime.value?.title ?: ""
val name = viewModel.currentEpisode.value?.name ?: ""
val thumbnailUrl = viewModel.currentAnime.value?.thumbnailUrl ?: ""

private fun buildMediaInfo(): MediaInfo {
val movieMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE)
title.let { movieMetadata.putString(MediaMetadata.KEY_TITLE, it) }
name.let { movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, it) }
movieMetadata.addImage(WebImage(Uri.parse(thumbnailUrl)))

return viewModel.currentEpisode.value?.url?.let { url ->
MediaInfo.Builder(url)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
// agrega varios tipos de videos
.setContentType("video/mp4")
.setContentUrl(url)
.setMetadata(movieMetadata)
.setStreamDuration((player.duration!!).toLong())
.build()
}!!
val currentAnime = viewModel.currentAnime.value ?: throw IllegalStateException("Anime data not available")
val currentvideo = viewModel.videoList.value[viewModel.selectedVideoIndex.value]
val cuarenteEpisode =
viewModel.currentEpisode.value ?: throw IllegalStateException("Episode data not available")

val movieMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE).apply {
putString(MediaMetadata.KEY_TITLE, currentAnime.title)
putString(MediaMetadata.KEY_SUBTITLE, cuarenteEpisode.name)
addImage(WebImage(Uri.parse(currentAnime.thumbnailUrl)))
}
val videoUrl = currentvideo.videoUrl ?: throw IllegalStateException("Video URL not available")

return MediaInfo.Builder(videoUrl)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType("video/mp4")
.setMetadata(movieMetadata)
.setStreamDuration((player.duration ?: 0).toLong())
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ fun PlayerControls(
isEpisodeOnline = viewModel.isEpisodeOnline(),
onMoreClick = { onOpenSheet(Sheets.More) },
onMoreLongClick = { onOpenPanel(Panels.VideoFilters) },
isCastEnabled = { playerPreferences.enableCast().get() },
)
}
// Bottom right controls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.mediarouter.app.MediaRouteButton
import com.google.android.gms.cast.framework.CastButtonFactory
import eu.kanade.tachiyomi.ui.player.controls.components.AutoPlaySwitch
import eu.kanade.tachiyomi.ui.player.controls.components.ControlsButton
import tachiyomi.presentation.core.components.material.padding
Expand All @@ -57,6 +60,9 @@ fun TopRightPlayerControls(
onMoreClick: () -> Unit,
onMoreLongClick: () -> Unit,

// cast
isCastEnabled: () -> Boolean,

modifier: Modifier = Modifier,
) {
Row(
Expand All @@ -71,6 +77,17 @@ fun TopRightPlayerControls(
.padding(vertical = MaterialTheme.padding.medium, horizontal = MaterialTheme.padding.mediumSmall)
.size(width = 48.dp, height = 24.dp),
)
if (isCastEnabled()) {
AndroidView(
factory = { context ->
MediaRouteButton(context).apply {
CastButtonFactory.setUpMediaRouteButton(context, this)
}
},
modifier = Modifier
.padding(vertical = MaterialTheme.padding.medium, horizontal = MaterialTheme.padding.mediumSmall),
)
}
ControlsButton(
icon = Icons.Default.Subtitles,
onClick = onSubtitlesClick,
Expand Down

0 comments on commit 0151c66

Please sign in to comment.