From 43a8d6095b7ca5027ad39166a79faf945f184460 Mon Sep 17 00:00:00 2001 From: Peter Collins Date: Tue, 18 Jun 2024 12:23:12 -0400 Subject: [PATCH] Hook up load media button --- .../GoogleCast.Binding/ApiDefinitions.cs | 4 ++ .../MauiGoogleCast/MauiGoogleCast.swift | 45 +++++++++++-------- googlecast/macios/sample/MainPage.xaml | 7 +++ googlecast/macios/sample/MainPage.xaml.cs | 21 ++++++--- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/googlecast/macios/GoogleCast.Binding/ApiDefinitions.cs b/googlecast/macios/GoogleCast.Binding/ApiDefinitions.cs index 974f131..07c5c33 100644 --- a/googlecast/macios/GoogleCast.Binding/ApiDefinitions.cs +++ b/googlecast/macios/GoogleCast.Binding/ApiDefinitions.cs @@ -14,6 +14,10 @@ interface GoogleCastManager // -(void)loadMediaWithUrl:(NSString * _Nonnull)url contentType:(NSString * _Nonnull)contentType title:(NSString * _Nonnull)title subtitle:(NSString * _Nonnull)subtitle imageUrl:(NSString * _Nonnull)imageUrl imageHeight:(NSInteger)imageHeight imageWidth:(NSInteger)imageWidth; [Export ("loadMediaWithUrl:contentType:title:subtitle:imageUrl:imageHeight:imageWidth:")] void LoadMedia (string url, string contentType, string title, string subtitle, string imageUrl, nint imageHeight, nint imageWidth); + + // +(BOOL)getIsCastSessionActive __attribute__((warn_unused_result(""))); + [Export ("getIsCastSessionActive")] + bool IsCastSessionActive { get; } } diff --git a/googlecast/macios/native/MauiGoogleCast/MauiGoogleCast.swift b/googlecast/macios/native/MauiGoogleCast/MauiGoogleCast.swift index d523b7c..f70e0d2 100644 --- a/googlecast/macios/native/MauiGoogleCast/MauiGoogleCast.swift +++ b/googlecast/macios/native/MauiGoogleCast/MauiGoogleCast.swift @@ -13,24 +13,28 @@ public class GoogleCastManager : NSObject, GCKLoggerDelegate, GCKRequestDelegate let kReceiverAppID = kGCKDefaultMediaReceiverApplicationID let kDebugLoggingEnabled = true - + @objc public func configure() { let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID) - let options = GCKCastOptions(discoveryCriteria: criteria) - GCKCastContext.setSharedInstanceWith(options) - - // Enable logger. - GCKLogger.sharedInstance().delegate = self - + let options = GCKCastOptions(discoveryCriteria: criteria) + GCKCastContext.setSharedInstanceWith(options) + + // Enable logger. + GCKLogger.sharedInstance().delegate = self } @objc public func loadMedia(url: String, contentType: String, title: String, subtitle: String, imageUrl: String, imageHeight: Int, imageWidth: Int) { + if !getIsCastSessionActive() { + logMessage("cast session not found, please start one", at: GCKLoggerLevel.error, fromFunction: "loadMedia", location: "line 31") + return + } + let url = URL.init(string: url) guard let mediaURL = url else { - print("invalid mediaURL") - return + logMessage("invalid mediaURL", at: GCKLoggerLevel.error, fromFunction: "loadMedia", location: "line 36") + return } let metadata = GCKMediaMetadata() @@ -39,28 +43,31 @@ public class GoogleCastManager : NSObject, GCKLoggerDelegate, GCKRequestDelegate metadata.addImage(GCKImage(url: URL(string: imageUrl)!, width: imageHeight, height: imageWidth)) - + let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: mediaURL) mediaInfoBuilder.streamType = GCKMediaStreamType.none; mediaInfoBuilder.contentType = contentType //eg: video/mp4 mediaInfoBuilder.metadata = metadata; let mediaInformation = mediaInfoBuilder.build() - - let sessionManager = GCKCastContext.sharedInstance().sessionManager - if let request = sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInformation) { - request.delegate = self + + if let request = GCKCastContext.sharedInstance().sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInformation) { + request.delegate = self } } + @objc + public func getIsCastSessionActive() -> Bool { + return GCKCastContext.sharedInstance().sessionManager.currentSession != nil + } public func logMessage(_ message: String, - at level: GCKLoggerLevel, - fromFunction function: String, - location: String) { + at level: GCKLoggerLevel, + fromFunction function: String, + location: String) { if (kDebugLoggingEnabled) { - print(function + " - " + message) + print(function + " - " + message) } - } + } } diff --git a/googlecast/macios/sample/MainPage.xaml b/googlecast/macios/sample/MainPage.xaml index 4c8580f..0246c5e 100644 --- a/googlecast/macios/sample/MainPage.xaml +++ b/googlecast/macios/sample/MainPage.xaml @@ -18,6 +18,13 @@ Style="{StaticResource SubHeadline}"/> + +