package com.applicaster.oasisplayer.chromecast import android.content.Context import android.graphics.Color import android.util.Log import androidx.lifecycle.Lifecycle import com.applicaster.app.CustomApplication import com.applicaster.oasisplayer.player.PlayMode import com.applicaster.oasisplayer.player.PlayerHolder import com.applicaster.oasisplayer.player.PlayerLifecycleListener import de.prosiebensat1digital.oasisplayer.player_api.OasisPlayer class OasisCastPlayer(private val playerHolder: PlayerHolder?) : PlayerLifecycleListener { // private var castPlugin: CastPlugin? = null // private var castStateListener: CastStateListener? = null override fun onPlayerInitStarted() { playerHolder?.let { val lifecycle = it.getCasterLifecycle() if (lifecycle != null) initCastPlugin(it.getCasterContext(), lifecycle) } } private fun initCastPlugin(context: Context, lifecycle: Lifecycle) { // castPlugin = ChromecastManager.getInstance().chromecastPlugin // try { // val castContext = CastContext.getSharedInstance(CustomApplication.getAppContext()) // castPlugin?.apply { // val playerConf = playerConfig ?: mutableMapOf() // setUpCastStateListener(castContext) // init(context, lifecycle, playerConf, this@OasisCastPlayer) // if (shouldPlayWithCast()) { // startCasting(playerConf) // playerHolder?.onCastStarted(getCastDeviceName(castContext)) // } // } // } catch (e: Exception) { // Log.e("OasisCastPlayer", "OasisCastPlayer cast error: $e.message") // } } override fun onPlayerCreated(oasisPlayer: OasisPlayer) { playerHolder?.let { it.hideMinimizedCastView() addChromecastBtn(it.getCasterContext(), oasisPlayer) } } override fun onPlayerDestroyed() { // if (castStateListener != null) { // val castContext = CastContext.getSharedInstance(CustomApplication.getAppContext()) // castContext.removeCastStateListener(castStateListener) // castStateListener = null // } } private fun addChromecastBtn(context: Context, oasisPlayer: OasisPlayer) { // if (castPlugin != null) { // CastButtonInjector().inject(context, oasisPlayer) { castButton -> // castPlugin?.initCastButton(castButton, Color.WHITE) // } // } } fun getPlayerConfig(): MutableMap? { return playerHolder?.let { CastPlayerConfigBuilder().build(it) } ?: mutableMapOf() } //CastPlugin event that casting started fun finishPlayer() { // val castContext = CastContext.getSharedInstance(CustomApplication.getAppContext()) // playerHolder?.onCastStarted(getCastDeviceName(castContext)) } fun shouldShowCustomExpandedCastingView(): Boolean { // show custom cast UI for inline player return playerHolder?.getCurrentPlayMode() == PlayMode.INLINE } fun launchCustomExpandedCastingView() { if (playerHolder?.getCurrentPlayMode() == PlayMode.INLINE) finishPlayer() } // private fun getCastDeviceName(castContext: CastContext): String? { // return castContext.sessionManager?.currentCastSession?.castDevice?.friendlyName // } // private fun setUpCastStateListener(castContext: CastContext) { // if (castStateListener == null) { // castStateListener = CastStateListener { castState -> // when (castState) { // CastState.CONNECTED -> { // Log.d("OasisCastPlayer", "OasisCastPlayer state CONNECTED + $castState") // finishPlayer() // } // CastState.NOT_CONNECTED -> { // val remoteMediaClient = castContext.sessionManager?.currentCastSession?.remoteMediaClient // val positionMillis = remoteMediaClient?.mediaStatus?.streamPosition // ?: remoteMediaClient?.approximateStreamPosition // Log.d("OasisCastPlayer", "OasisCastPlayer state DISCONNECTED $castState + $positionMillis") // playerHolder?.onCastStopped(positionMillis) // } // } // } // castContext.addCastStateListener(castStateListener) // } // } }