allow old cast method

This commit is contained in:
Finnley Somdahl 2024-01-12 20:54:34 -06:00
parent f12a4de04b
commit 419d33a3ac

View file

@ -4,7 +4,6 @@ import android.animation.ObjectAnimator
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog import android.app.AlertDialog
import android.app.Dialog import android.app.Dialog
import android.app.DownloadManager
import android.app.PictureInPictureParams import android.app.PictureInPictureParams
import android.app.PictureInPictureUiState import android.app.PictureInPictureUiState
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
@ -31,7 +30,6 @@ import android.view.*
import android.view.KeyEvent.* import android.view.KeyEvent.*
import android.view.animation.AnimationUtils import android.view.animation.AnimationUtils
import android.widget.AdapterView import android.widget.AdapterView
import android.widget.FrameLayout
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.Spinner import android.widget.Spinner
import android.widget.TextView import android.widget.TextView
@ -43,8 +41,9 @@ import androidx.core.content.res.ResourcesCompat
import androidx.core.math.MathUtils.clamp import androidx.core.math.MathUtils.clamp
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.media3.cast.CastPlayer
import androidx.media3.cast.SessionAvailabilityListener
import androidx.media3.common.* import androidx.media3.common.*
import androidx.media3.common.C.AUDIO_CONTENT_TYPE_MOVIE import androidx.media3.common.C.AUDIO_CONTENT_TYPE_MOVIE
import androidx.media3.common.C.TRACK_TYPE_VIDEO import androidx.media3.common.C.TRACK_TYPE_VIDEO
@ -60,6 +59,7 @@ import androidx.media3.exoplayer.util.EventLogger
import androidx.media3.session.MediaSession import androidx.media3.session.MediaSession
import androidx.media3.ui.* import androidx.media3.ui.*
import androidx.media3.ui.CaptionStyleCompat.* import androidx.media3.ui.CaptionStyleCompat.*
import androidx.mediarouter.app.MediaRouteButton
import ani.dantotsu.* import ani.dantotsu.*
import ani.dantotsu.R import ani.dantotsu.R
import ani.dantotsu.connections.anilist.Anilist import ani.dantotsu.connections.anilist.Anilist
@ -69,7 +69,7 @@ import ani.dantotsu.connections.discord.DiscordServiceRunningSingleton
import ani.dantotsu.connections.discord.RPC import ani.dantotsu.connections.discord.RPC
import ani.dantotsu.connections.updateProgress import ani.dantotsu.connections.updateProgress
import ani.dantotsu.databinding.ActivityExoplayerBinding import ani.dantotsu.databinding.ActivityExoplayerBinding
import ani.dantotsu.media.anime.AnimeNameAdapter import ani.dantotsu.download.video.Helper
import ani.dantotsu.media.Media import ani.dantotsu.media.Media
import ani.dantotsu.media.MediaDetailsViewModel import ani.dantotsu.media.MediaDetailsViewModel
import ani.dantotsu.media.SubtitleDownloader import ani.dantotsu.media.SubtitleDownloader
@ -85,6 +85,10 @@ import ani.dantotsu.settings.PlayerSettingsActivity
import ani.dantotsu.settings.UserInterfaceSettings import ani.dantotsu.settings.UserInterfaceSettings
import ani.dantotsu.themes.ThemeManager import ani.dantotsu.themes.ThemeManager
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.google.android.gms.cast.framework.CastButtonFactory
import com.google.android.gms.cast.framework.CastContext
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.material.slider.Slider import com.google.android.material.slider.Slider
import com.google.firebase.crashlytics.FirebaseCrashlytics import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.lagradost.nicehttp.ignoreAllSSLErrors import com.lagradost.nicehttp.ignoreAllSSLErrors
@ -99,14 +103,6 @@ import java.util.concurrent.*
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
import kotlin.math.roundToInt import kotlin.math.roundToInt
import androidx.media3.cast.SessionAvailabilityListener
import androidx.media3.cast.CastPlayer
import androidx.media3.exoplayer.offline.Download
import androidx.mediarouter.app.MediaRouteButton
import ani.dantotsu.download.video.Helper
import com.google.android.gms.cast.framework.CastButtonFactory
import com.google.android.gms.cast.framework.CastContext
import com.google.android.material.snackbar.Snackbar
@UnstableApi @UnstableApi
@SuppressLint("SetTextI18n", "ClickableViewAccessibility") @SuppressLint("SetTextI18n", "ClickableViewAccessibility")
@ -118,8 +114,9 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
private val playerOnPlay = "playerOnPlay" private val playerOnPlay = "playerOnPlay"
private lateinit var exoPlayer: ExoPlayer private lateinit var exoPlayer: ExoPlayer
private lateinit var castPlayer: CastPlayer private var castPlayer: CastPlayer? = null
private lateinit var castContext: CastContext private var castContext: CastContext? = null
private var isCastApiAvailable = false
private lateinit var trackSelector: DefaultTrackSelector private lateinit var trackSelector: DefaultTrackSelector
private lateinit var cacheFactory: CacheDataSource.Factory private lateinit var cacheFactory: CacheDataSource.Factory
private lateinit var playbackParameters: PlaybackParameters private lateinit var playbackParameters: PlaybackParameters
@ -342,10 +339,14 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
setContentView(binding.root) setContentView(binding.root)
//Initialize //Initialize
isCastApiAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS
try {
castContext = CastContext.getSharedInstance(this) castContext = CastContext.getSharedInstance(this)
castPlayer = CastPlayer(castContext) castPlayer = CastPlayer(castContext!!)
castPlayer.setSessionAvailabilityListener(this) castPlayer!!.setSessionAvailabilityListener(this)
} catch (e: Exception) {
isCastApiAvailable = false
}
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
hideSystemBars() hideSystemBars()
@ -484,14 +485,14 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
if (isInitialized) { if (isInitialized) {
isPlayerPlaying = exoPlayer.isPlaying isPlayerPlaying = exoPlayer.isPlaying
(exoPlay.drawable as Animatable?)?.start() (exoPlay.drawable as Animatable?)?.start()
if (isPlayerPlaying || castPlayer.isPlaying) { if (isPlayerPlaying || castPlayer?.isPlaying == true) {
Glide.with(this).load(R.drawable.anim_play_to_pause).into(exoPlay) Glide.with(this).load(R.drawable.anim_play_to_pause).into(exoPlay)
exoPlayer.pause() exoPlayer.pause()
castPlayer.pause() castPlayer?.pause()
} else { } else {
if (!castPlayer.isPlaying && castPlayer.currentMediaItem != null) { if (castPlayer?.isPlaying == false && castPlayer?.currentMediaItem != null) {
Glide.with(this).load(R.drawable.anim_pause_to_play).into(exoPlay) Glide.with(this).load(R.drawable.anim_pause_to_play).into(exoPlay)
castPlayer.play() castPlayer?.play()
} else if (!isPlayerPlaying) { } else if (!isPlayerPlaying) {
Glide.with(this).load(R.drawable.anim_pause_to_play).into(exoPlay) Glide.with(this).load(R.drawable.anim_pause_to_play).into(exoPlay)
exoPlayer.play() exoPlayer.play()
@ -957,7 +958,14 @@ episodes.forEach {
} }
val regexPattern = Regex(AnimeNameAdapter.episodeRegex, RegexOption.IGNORE_CASE) val regexPattern = Regex(AnimeNameAdapter.episodeRegex, RegexOption.IGNORE_CASE)
episodeTitleArr.replaceAll { it.replace(regexPattern, "") }
for (i in episodeTitleArr.indices) {
val replaced = episodeTitleArr[i].replace(regexPattern, "")
if (replaced.isNotBlank()) {
episodeTitleArr[i] = replaced
}
}
//Episode Change //Episode Change
fun change(index: Int) { fun change(index: Int) {
@ -1104,8 +1112,16 @@ episodeTitleArr.replaceAll { it.replace(regexPattern, "") }
if (settings.cast) { if (settings.cast) {
playerView.findViewById<MediaRouteButton>(R.id.exo_cast).apply { playerView.findViewById<MediaRouteButton>(R.id.exo_cast).apply {
visibility = View.VISIBLE visibility = View.VISIBLE
try {
CastButtonFactory.setUpMediaRouteButton(context, this) CastButtonFactory.setUpMediaRouteButton(context, this)
dialogFactory = CustomCastThemeFactory() dialogFactory = CustomCastThemeFactory()
} catch (e: Exception) {
isCastApiAvailable = false
}
setOnLongClickListener {
cast()
true
}
} }
} }
@ -1543,7 +1559,7 @@ episodeTitleArr.replaceAll { it.replace(regexPattern, "") }
super.onPause() super.onPause()
orientationListener?.disable() orientationListener?.disable()
if (isInitialized) { if (isInitialized) {
if (!castPlayer.isPlaying) { if (castPlayer?.isPlaying == false) {
playerView.player?.pause() playerView.player?.pause()
} }
saveData( saveData(
@ -1566,7 +1582,7 @@ episodeTitleArr.replaceAll { it.replace(regexPattern, "") }
} }
override fun onStop() { override fun onStop() {
if (!castPlayer.isPlaying) { if (castPlayer?.isPlaying == false) {
playerView.player?.pause() playerView.player?.pause()
} }
super.onStop() super.onStop()
@ -1935,11 +1951,15 @@ episodeTitleArr.replaceAll { it.replace(regexPattern, "") }
private fun startCastPlayer() { private fun startCastPlayer() {
castPlayer.setMediaItem(mediaItem) if (!isCastApiAvailable) {
castPlayer.prepare() snackString("Cast API not available")
return
}
castPlayer?.setMediaItem(mediaItem)
castPlayer?.prepare()
playerView.player = castPlayer playerView.player = castPlayer
exoPlayer.stop() exoPlayer.stop()
castPlayer.addListener(object : Player.Listener { castPlayer?.addListener(object : Player.Listener {
//if the player is paused changed, we want to update the UI //if the player is paused changed, we want to update the UI
override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) { override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
super.onPlayWhenReadyChanged(playWhenReady, reason) super.onPlayWhenReadyChanged(playWhenReady, reason)
@ -1962,12 +1982,14 @@ episodeTitleArr.replaceAll { it.replace(regexPattern, "") }
exoPlayer.setMediaItem(mediaItem) exoPlayer.setMediaItem(mediaItem)
exoPlayer.prepare() exoPlayer.prepare()
playerView.player = exoPlayer playerView.player = exoPlayer
castPlayer.stop() castPlayer?.stop()
} }
override fun onCastSessionAvailable() { override fun onCastSessionAvailable() {
if (isCastApiAvailable) {
startCastPlayer() startCastPlayer()
} }
}
override fun onCastSessionUnavailable() { override fun onCastSessionUnavailable() {
startExoPlayer() startExoPlayer()