-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlayerPageRenderer.cs
247 lines (199 loc) · 7.72 KB
/
PlayerPageRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Diagnostics;
using AVFoundation;
using AVKit;
using CoreGraphics;
using Foundation;
using Google.Cast;
using Microsoft.MobCAT;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using XamCast;
using XamCast.iOS.Renderers;
[assembly: ExportRenderer(typeof(PlayerPage), typeof(PlayerPageRenderer))]
namespace XamCast.iOS.Renderers
{
public class PlayerPageRenderer : PageRenderer
{
Lazy<IChromecastService> chromecastService = new Lazy<IChromecastService>(() => ServiceContainer.Resolve<IChromecastService>());
XamCast.Models.MediaInfo mediaInfo;
AVPlayer avp;
AVPlayerViewController avpvc;
NSUrl url;
public SessionManager sessionManager;
public SessionManagerListener xamaSessionManagerListener;
public UIMediaController castMediaController;
public MediaInformation castMediaInfo;
public VisualElement currentPage;
// track state as sample does here https://github.com/googlecast/CastVideos-ios/blob/master/CastVideos-swift/Classes/MediaViewController.swift#L18
private PlaybackLocation mLocation;
public enum PlaybackLocation
{
LOCAL,
REMOTE
}
public PlayerPageRenderer()
{
mediaInfo = chromecastService.Value.GetPlaybackAsset();
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
try
{
SetupChromecastThings();
SetupUserInterface();
StartVideoPlayback();
currentPage = Element;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"\t\t\tERROR: {ex.Message}");
}
void StartVideoPlayback()
{
var hasConnection = sessionManager.HasConnectedSession;
if (hasConnection)
{
mLocation = PlaybackLocation.REMOTE;
SwitchToRemotePlayback();
ClosePage();
}
if (mLocation == PlaybackLocation.LOCAL)
avp.Play();
}
void SetupUserInterface()
{
url = NSUrl.FromString(mediaInfo.SourceURL);
avp = new AVPlayer(url);
avpvc = new AVPlayerViewController();
avpvc.Player = avp;
AddChildViewController(avpvc);
avpvc.View.Frame = new CGRect(0, 100, 375, 300);
avpvc.ShowsPlaybackControls = true;
View.AddSubview(avpvc.View);
var castButton = new UICastButton(new CGRect(50, 20, 24, 24));
View.AddSubview(castButton);
}
void SetupChromecastThings()
{
//setup Cast session manager
sessionManager = CastContext.SharedInstance.SessionManager;
xamaSessionManagerListener = new XamSessionManagerListener(this);
sessionManager.AddListener(xamaSessionManagerListener);
//castMediaController
castMediaController = new UIMediaController();
castMediaController.Delegate = new XamMediaControllerDelegate();
}
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
avp.Pause();
avp.Dispose();
avpvc.Dispose();
}
public void ClosePage()
{
currentPage.Navigation.PopAsync();
}
//CreateCastMedia
public void CreateMediaInfo()
{
var videoUrl = mediaInfo.SourceURL;
var vname = mediaInfo.DisplayName;
var extra = new string[] { "{", "}" };
var metaData = new MediaMetadata(MediaMetadataType.Generic);
metaData.SetString(mediaInfo.DisplayName, MetadataKey.Title);
metaData.SetString("Hello World on Fridays!", MetadataKey.Subtitle);
var builder = new MediaInformationBuilder();
builder.ContentId = videoUrl;
builder.StreamType = MediaStreamType.Buffered;
builder.Metadata = metaData;
castMediaInfo = builder.Build();
}
public void SwitchToLocalPlayback(NSError withError)
{
Debug.WriteLine("OOPSY Happened to Cast Connect! :: " + withError);
// set remotemediaclient to null
//add logic to resume local play, update tracking
}
public void SwitchToRemotePlayback()
{
avp.Pause();
CreateMediaInfo();
mLocation = PlaybackLocation.REMOTE;
//ios sample does it via queue, we'll straight up load
var castSession = CastContext.SharedInstance.SessionManager.CurrentCastSession;
RemoteMediaClient remoteMediaClient = null;
var options = new MediaLoadOptions();
//options.PlayPosition = currentprogress;
options.Autoplay = true;
if (castSession != null)
{
remoteMediaClient = castSession.RemoteMediaClient;
remoteMediaClient.MediaQueue?.Clear();
}
if (castSession != null && remoteMediaClient != null && castMediaInfo != null)
remoteMediaClient.LoadMedia(castMediaInfo, options);
else
Console.WriteLine("ERROR in SwitchToRemotePlayback");
ClosePage();
}
}
// MARK: - GCKSessionManagerListener
public class XamSessionManagerListener : SessionManagerListener
{
PlayerPageRenderer playerPage;
public XamSessionManagerListener(PlayerPageRenderer pageRenderer)
{
playerPage = pageRenderer;
}
public override void DidStartSession(SessionManager sessionManager, Session session)
{
playerPage.SwitchToRemotePlayback();
}
public override void DidResumeSession(SessionManager sessionManager, Session session)
{
playerPage.SwitchToRemotePlayback();
}
public override void DidEndSession(SessionManager sessionManager, Session session, NSError error)
{
playerPage.SwitchToLocalPlayback(error);
}
public override void DidFailToStartSession(SessionManager sessionManager, Session session, NSError error)
{
playerPage.SwitchToLocalPlayback(error);
}
}
// MARK: - GCKUIMediaControllerDelegate
public class XamMediaControllerDelegate : UIMediaControllerDelegate
{
public XamMediaControllerDelegate()
{
}
public override void DidUpdateMediaStatus(UIMediaController mediaController, MediaStatus mediaStatus)
{
// Once the video has finished, let the delegate know
// and attempt to proceed to the next session, if autoAdvance
// is enabled
if (mediaStatus != null)
{
if (mediaStatus.IdleReason == MediaPlayerIdleReason.Finished)
{
//add own logic to resume local play, update tracking etc as needed
return;
}
if (mediaStatus.IdleReason == MediaPlayerIdleReason.Error)
{
Debug.WriteLine("OOPSY Happened to Cast Playback! :: " + MediaPlayerIdleReason.Error);
return;
}
}
}
}
}