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}"/>
+
+
+
diff --git a/googlecast/macios/sample/MainPage.xaml.cs b/googlecast/macios/sample/MainPage.xaml.cs
index 7f0edda..1688679 100644
--- a/googlecast/macios/sample/MainPage.xaml.cs
+++ b/googlecast/macios/sample/MainPage.xaml.cs
@@ -26,12 +26,19 @@ protected override async void OnAppearing()
void OnLoadMediaBtnClicked(object sender, EventArgs e)
{
- googleCastManager?.LoadMedia("https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4",
- "video/mp4",
- "Big Buck Bunny",
- "Big Buck Bunny (open-source movie)",
- "https://peach.blender.org/wp-content/uploads/title_anouncement.jpg",
- 100,
- 100);
+ if (!googleCastManager?.IsCastSessionActive ?? true)
+ {
+ DisplayAlert("Error", "Please tap the cast button to begin casting.", "OK");
+ return;
+ }
+
+ googleCastManager?.LoadMedia(
+ "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
+ "video/mp4",
+ "Big Buck Bunny (2008)",
+ "Big Buck Bunny tells the story of a giant rabbit with a heart bigger than himself.",
+ "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg",
+ 480,
+ 360);
}
}