From 447bdd576db39f336abe58fac018dbf50ed4cb5b Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Tue, 30 Nov 2021 06:50:25 +0100 Subject: [PATCH] RemoteMediaClient playerState android, ios, flutter --- .../FlutterCastFrameworkPlugin.kt | 4 +- example/lib/main.dart | 4 +- .../SwiftFlutterCastFrameworkPlugin.swift | 67 ++++++++++--------- lib/src/cast/SessionManager.dart | 21 +++++- lib/src/flutter_cast_framework.dart | 5 +- 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt index 52e9772..429a0bc 100644 --- a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt +++ b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt @@ -12,6 +12,7 @@ import com.gianlucaparadise.flutter_cast_framework.cast.MessageCastingChannel import com.gianlucaparadise.flutter_cast_framework.media.getFlutterMediaInfo import com.gianlucaparadise.flutter_cast_framework.media.getMediaLoadRequestData import com.google.android.gms.cast.MediaError +import com.google.android.gms.cast.MediaStatus.PLAYER_STATE_UNKNOWN import com.google.android.gms.cast.framework.CastContext import com.google.android.gms.cast.framework.CastSession import com.google.android.gms.cast.framework.SessionManager @@ -177,7 +178,8 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa override fun onStatusUpdated() { Log.d(TAG, "RemoteMediaClient - onStatusUpdated") super.onStatusUpdated() - flutterApi?.onStatusUpdated { } + val playerStateRaw = remoteMediaClient?.playerState ?: PLAYER_STATE_UNKNOWN + flutterApi?.onStatusUpdated(playerStateRaw.toLong()) { } } override fun onMetadataUpdated() { diff --git a/example/lib/main.dart b/example/lib/main.dart index ac5fd69..29c02b1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -68,8 +68,8 @@ class _MyAppState extends State { }); } - void _onRemoteMediaClientStatusUpdated() { - debugPrint("RemoteMediaClient status updated"); + void _onRemoteMediaClientStatusUpdated(PlayerState playerState) { + debugPrint("RemoteMediaClient status updated - playerState $playerState"); } void _onSendMessage() { diff --git a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift index 016f2ec..95594e9 100644 --- a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift +++ b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift @@ -29,7 +29,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio var castSession: GCKCastSession? { get { return _castSession } set { - print("Updating castSession - castSession changed: \(_castSession != newValue)") + debugPrint("Updating castSession - castSession changed: \(_castSession != newValue)") let oldSession = _castSession let newSession = newValue @@ -39,7 +39,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio remoteMediaClient = newValue?.remoteMediaClient flutterApi.getSessionMessageNamespaces { (namespaces, err) in - print("Updating castSession - getSessionMessageNamespaces success - param: \(namespaces.joined(separator: ", "))") + debugPrint("Updating castSession - getSessionMessageNamespaces success - param: \(namespaces.joined(separator: ", "))") if (oldSession == nil && newSession == nil) { return // nothing to do here } @@ -68,7 +68,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio var remoteMediaClient: GCKRemoteMediaClient? { get { return _remoteMediaClient } set { - print("Updating remoteMediaClient - remoteMediaClient changed: \(_remoteMediaClient != newValue)") + debugPrint("Updating remoteMediaClient - remoteMediaClient changed: \(_remoteMediaClient != newValue)") _remoteMediaClient?.remove(self) newValue?.add(self) @@ -98,7 +98,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio } @objc func appDidBecomeActive() { - print("AppLife: appDidBecomeActive - App moved to foreground!") + debugPrint("AppLife: appDidBecomeActive - App moved to foreground!") self.sessionManager.add(self) self.castSession = self.sessionManager.currentCastSession @@ -106,14 +106,14 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio } @objc func appWillResignActive() { - print("AppLife: appWillResignActive - App moved to background!") + debugPrint("AppLife: appWillResignActive - App moved to background!") self.sessionManager.remove(self) self.castSession = nil } private func onCastStateChanged(state: GCKCastContext, change: NSKeyValueObservedChange) { let castState = GCKCastContext.sharedInstance().castState - print("cast state change to: \(castState.rawValue)") + debugPrint("cast state change to: \(castState.rawValue)") notifyCastState(castState: castState) } @@ -219,14 +219,14 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionSuspended public func sessionManager(_ sessionManager: GCKSessionManager, didSuspend session: GCKCastSession, with reason: GCKConnectionSuspendReason) { - print("SessionListener: didSuspend") + debugPrint("SessionListener: didSuspend") flutterApi.onSessionSuspended { (_:Error?) in } } // onSessionStarting public func sessionManager(_ sessionManager: GCKSessionManager, willStart session: GCKCastSession) { - print("SessionListener: willStart") + debugPrint("SessionListener: willStart") flutterApi.onSessionStarting { (_:Error?) in } @@ -235,7 +235,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionResuming public func sessionManager(_ sessionManager: GCKSessionManager, willResumeCastSession session: GCKCastSession) { - print("SessionListener: willResumeCastSession") + debugPrint("SessionListener: willResumeCastSession") flutterApi.onSessionResuming { (_:Error?) in } @@ -244,7 +244,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionEnding public func sessionManager(_ sessionManager: GCKSessionManager, willEnd session: GCKCastSession) { - print("SessionListener: willEnd") + debugPrint("SessionListener: willEnd") stopProgressTImer() flutterApi.onSessionEnding { (_:Error?) in } @@ -252,7 +252,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionStartFailed public func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKCastSession, withError error: Error) { - print("SessionListener: didFailToStart") + debugPrint("SessionListener: didFailToStart") flutterApi.onSessionStartFailed { (_:Error?) in } } @@ -261,7 +261,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionStarted public func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKCastSession) { - print("SessionListener: didStart") + debugPrint("SessionListener: didStart") flutterApi.onSessionStarted { (_:Error?) in } @@ -270,7 +270,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionResumed public func sessionManager(_ sessionManager: GCKSessionManager, didResumeCastSession session: GCKCastSession) { - print("SessionListener: didResumeCastSession") + debugPrint("SessionListener: didResumeCastSession") flutterApi.onSessionResumed { (_:Error?) in } @@ -279,52 +279,53 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionEnded public func sessionManager(_ sessionManager: GCKSessionManager, didEnd session: GCKCastSession, withError error: Error?) { - print("SessionListener: didEnd") + debugPrint("SessionListener: didEnd") flutterApi.onSessionEnded { (_:Error?) in } } // onQueueStatusUpdated public func remoteMediaClientDidUpdateQueue(_ client: GCKRemoteMediaClient) { - print("RemoteMediaClientListener: didUpdateQueue") + debugPrint("RemoteMediaClientListener: didUpdateQueue") flutterApi.onQueueStatusUpdated { (_:Error?) in } } // onPreloadStatusUpdated public func remoteMediaClientDidUpdatePreloadStatus(_ client: GCKRemoteMediaClient) { - print("RemoteMediaClientListener: didUpdatePreloadStatus") + debugPrint("RemoteMediaClientListener: didUpdatePreloadStatus") flutterApi.onPreloadStatusUpdated { (_:Error?) in } } // onStatusUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didUpdate mediaStatus: GCKMediaStatus?) { - var playerState = "" + var playerStateLabel = "" switch mediaStatus?.playerState { case .unknown: - playerState = "PlayerStateUnknown" + playerStateLabel = "PlayerStateUnknown" case .idle: - playerState = "PlayerStateIdle" + playerStateLabel = "PlayerStateIdle" case .playing: - playerState = "PlayerStatePlaying" + playerStateLabel = "PlayerStatePlaying" startProgressTimer() case .paused: - playerState = "PlayerStatePaused" + playerStateLabel = "PlayerStatePaused" startProgressTimer() case .buffering: - playerState = "PlayerStateBuffering" + playerStateLabel = "PlayerStateBuffering" startProgressTimer() case .loading: - playerState = "PlayerStateLoading" + playerStateLabel = "PlayerStateLoading" startProgressTimer() default: break } - debugPrint("RemoteMediaClientListener: didUpdate mediaStatus - playerState: \(playerState)") - - flutterApi.onStatusUpdated { (_:Error?) in + debugPrint("RemoteMediaClientListener: didUpdate mediaStatus - playerState: \(playerStateLabel)") + let playerState = mediaStatus?.playerState ?? GCKMediaPlayerState.unknown + let nsPlayerState = NSNumber(value: playerState.rawValue) + flutterApi.onStatusUpdatedPlayerStateRaw(nsPlayerState) { (_:Error?) in } } @@ -333,49 +334,49 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onMetadataUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didUpdate mediaMetadata: GCKMediaMetadata?) { - print("RemoteMediaClientListener: didUpdate mediaMetadata") + debugPrint("RemoteMediaClientListener: didUpdate mediaMetadata") flutterApi.onMetadataUpdated { (_:Error?) in } } // onQueueStatusUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didReceive queueItems: [GCKMediaQueueItem]) { - print("RemoteMediaClientListener: didReceive queueItems") + debugPrint("RemoteMediaClientListener: didReceive queueItems") flutterApi.onQueueStatusUpdated { (_:Error?) in } } // onSendingRemoteMediaRequest public func remoteMediaClient(_ client: GCKRemoteMediaClient, didStartMediaSessionWithID sessionID: Int) { - print("RemoteMediaClientListener: didStartMediaSessionWithID") + debugPrint("RemoteMediaClientListener: didStartMediaSessionWithID") flutterApi.onSendingRemoteMediaRequest { (_:Error?) in } } // onQueueStatusUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didReceiveQueueItemIDs queueItemIDs: [NSNumber]) { - print("RemoteMediaClientListener: didReceiveQueueItemIDs") + debugPrint("RemoteMediaClientListener: didReceiveQueueItemIDs") flutterApi.onQueueStatusUpdated { (_:Error?) in } } // onQueueStatusUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didUpdateQueueItemsWithIDs queueItemIDs: [NSNumber]) { - print("RemoteMediaClientListener: didUpdateQueueItemsWithIDs") + debugPrint("RemoteMediaClientListener: didUpdateQueueItemsWithIDs") flutterApi.onQueueStatusUpdated { (_:Error?) in } } // onQueueStatusUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didRemoveQueueItemsWithIDs queueItemIDs: [NSNumber]) { - print("RemoteMediaClientListener: didRemoveQueueItemsWithIDs") + debugPrint("RemoteMediaClientListener: didRemoveQueueItemsWithIDs") flutterApi.onQueueStatusUpdated { (_:Error?) in } } // onQueueStatusUpdated public func remoteMediaClient(_ client: GCKRemoteMediaClient, didInsertQueueItemsWithIDs queueItemIDs: [NSNumber], beforeItemWithID beforeItemID: UInt) { - print("RemoteMediaClientListener: didInsertQueueItemsWithIDs") + debugPrint("RemoteMediaClientListener: didInsertQueueItemsWithIDs") flutterApi.onQueueStatusUpdated { (_:Error?) in } } diff --git a/lib/src/cast/SessionManager.dart b/lib/src/cast/SessionManager.dart index a0cb345..5b38521 100644 --- a/lib/src/cast/SessionManager.dart +++ b/lib/src/cast/SessionManager.dart @@ -7,7 +7,8 @@ class SessionManager { SessionManager(this._hostApi); - final ValueNotifier state = ValueNotifier(SessionState.idle); + final state = ValueNotifier(SessionState.idle); + final playerState = ValueNotifier(PlayerState.unknown); void onSessionStateChanged(SessionState sessionState) { switch (sessionState) { @@ -28,9 +29,14 @@ class SessionManager { } } + void dispatchOnPlayerStateUpdated(PlayerState playerState) { + this.playerState.value = playerState; + onStatusUpdated?.call(playerState); + } + MessageReceivedCallback? onMessageReceived; - VoidCallback? onStatusUpdated; + StatusUpdatedCallback? onStatusUpdated; VoidCallback? onMetadataUpdated; VoidCallback? onQueueStatusUpdated; VoidCallback? onPreloadStatusUpdated; @@ -80,3 +86,14 @@ enum SessionState { session_resume_failed, session_suspended, } + +typedef StatusUpdatedCallback = void Function(PlayerState); + +enum PlayerState { + unknown, // 0 + idle, // 1 + playing, // 2 + paused, // 3 + buffering, // 4 + loading, // 5 +} diff --git a/lib/src/flutter_cast_framework.dart b/lib/src/flutter_cast_framework.dart index 9a6e727..9af2af1 100644 --- a/lib/src/flutter_cast_framework.dart +++ b/lib/src/flutter_cast_framework.dart @@ -122,8 +122,9 @@ class FlutterCastFramework extends CastFlutterApi { } @override - void onStatusUpdated() { - castContext.sessionManager.onStatusUpdated?.call(); + void onStatusUpdated(int playerStateRaw) { + final playerState = PlayerState.values[playerStateRaw]; + castContext.sessionManager.dispatchOnPlayerStateUpdated(playerState); } @override