Skip to content

Commit

Permalink
Hook up load media button
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcollins committed Jun 18, 2024
1 parent 798cda6 commit 43a8d60
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
4 changes: 4 additions & 0 deletions googlecast/macios/GoogleCast.Binding/ApiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}


Expand Down
45 changes: 26 additions & 19 deletions googlecast/macios/native/MauiGoogleCast/MauiGoogleCast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
}
}
}


Expand Down
7 changes: 7 additions & 0 deletions googlecast/macios/sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
Style="{StaticResource SubHeadline}"/>

<local:GoogleCastButtonView x:Name="GoogleCastButton" />

<Button
x:Name="LoadMediaBtn"
Text="Cast Media"
Clicked="OnLoadMediaBtnClicked"
HorizontalOptions="Fill" />

</VerticalStackLayout>
</ScrollView>

Expand Down
21 changes: 14 additions & 7 deletions googlecast/macios/sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 43a8d60

Please sign in to comment.