Only auto relaunch player the first time ChromeCast is started, do not reset time to 0 if player is not found, stop casting if ChromeCast player is disconnected.

This commit is contained in:
Koen J 2025-07-24 12:23:23 +02:00
commit 769ec9f59a

View file

@ -62,6 +62,7 @@ class ChromecastCastingDevice : CastingDevice {
private val MAX_LAUNCH_RETRIES = 3 private val MAX_LAUNCH_RETRIES = 3
private var _lastLaunchTime_ms = 0L private var _lastLaunchTime_ms = 0L
private var _retryJob: Job? = null private var _retryJob: Job? = null
private var _autoLaunchEnabled = true
constructor(name: String, addresses: Array<InetAddress>, port: Int) : super() { constructor(name: String, addresses: Array<InetAddress>, port: Int) : super() {
this.name = name; this.name = name;
@ -305,6 +306,7 @@ class ChromecastCastingDevice : CastingDevice {
return; return;
} }
_autoLaunchEnabled = true
_started = true; _started = true;
_sessionId = null; _sessionId = null;
_launchRetries = 0 _launchRetries = 0
@ -546,6 +548,7 @@ class ChromecastCastingDevice : CastingDevice {
if (appId == "CC1AD845") { if (appId == "CC1AD845") {
sessionIsRunning = true; sessionIsRunning = true;
_autoLaunchEnabled = false
if (_sessionId == null) { if (_sessionId == null) {
connectionState = CastConnectionState.CONNECTED; connectionState = CastConnectionState.CONNECTED;
@ -568,21 +571,22 @@ class ChromecastCastingDevice : CastingDevice {
if (System.currentTimeMillis() - _lastLaunchTime_ms > 5000) { if (System.currentTimeMillis() - _lastLaunchTime_ms > 5000) {
_sessionId = null _sessionId = null
_mediaSessionId = null _mediaSessionId = null
setTime(0.0)
_transportId = null _transportId = null
if (_launching && _launchRetries < MAX_LAUNCH_RETRIES) { if (_autoLaunchEnabled) {
Logger.i(TAG, "No player yet; attempting launch #${_launchRetries + 1}") if (_launching && _launchRetries < MAX_LAUNCH_RETRIES) {
_launchRetries++ Logger.i(TAG, "No player yet; attempting launch #${_launchRetries + 1}")
launchPlayer() _launchRetries++
} else if (!_launching && _launchRetries < MAX_LAUNCH_RETRIES) { launchPlayer()
// Maybe the first GET_STATUS came back empty; still try launching } else {
Logger.i(TAG, "Player not found; triggering launch #${_launchRetries + 1}") // Maybe the first GET_STATUS came back empty; still try launching
_launching = true Logger.i(TAG, "Player not found; triggering launch #${_launchRetries + 1}")
_launchRetries++ _launching = true
launchPlayer() _launchRetries++
launchPlayer()
}
} else { } else {
Logger.e(TAG, "Player not found after $_launchRetries attempts; giving up.") Logger.e(TAG, "Player not found ($_launchRetries, _autoLaunchEnabled = $_autoLaunchEnabled); giving up.")
Logger.i(TAG, "Unable to start media receiver on device") Logger.i(TAG, "Unable to start media receiver on device")
stop() stop()
} }
@ -599,6 +603,7 @@ class ChromecastCastingDevice : CastingDevice {
} else { } else {
_launching = false _launching = false
_launchRetries = 0 _launchRetries = 0
_autoLaunchEnabled = false
} }
val volume = status.getJSONObject("volume"); val volume = status.getJSONObject("volume");
@ -639,7 +644,7 @@ class ChromecastCastingDevice : CastingDevice {
} else if (type == "CLOSE") { } else if (type == "CLOSE") {
if (message.sourceId == "receiver-0") { if (message.sourceId == "receiver-0") {
Logger.i(TAG, "Close received."); Logger.i(TAG, "Close received.");
stop(); stopCasting();
} else if (_transportId == message.sourceId) { } else if (_transportId == message.sourceId) {
throw Exception("Transport id closed.") throw Exception("Transport id closed.")
} }
@ -676,6 +681,10 @@ class ChromecastCastingDevice : CastingDevice {
localAddress = null; localAddress = null;
_started = false; _started = false;
_contentId = null
_contentType = null
_streamType = null
_retryJob?.cancel() _retryJob?.cancel()
_retryJob = null _retryJob = null