-
Notifications
You must be signed in to change notification settings - Fork 5
/
MyCastButtonRenderer.cs
75 lines (62 loc) · 2.49 KB
/
MyCastButtonRenderer.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
using System;
using Android.Content;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using XamCast;
using XamCast.Droid.Renderers;
using Android.Gms.Cast.Framework;
using Android.Support.V7.App;
using Android.Support.V7.Media;
using Android.Gms.Cast;
[assembly: ExportRenderer(typeof(MyCastButton), typeof(MyCastButtonRenderer))]
namespace XamCast.Droid.Renderers
{
public class MyCastButtonRenderer : ViewRenderer<MyCastButton, LinearLayout>
{
MediaRouteButton mediaRouteButton;
MediaRouter mediaRouter;
MediaRouteSelector mediaRouteSelector;
MediaRouter.Callback mediaRouterCallback;
LinearLayout linearLayout;
public MyCastButtonRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<MyCastButton> e)
{
base.OnElementChanged(e);
if(e.NewElement != null)
{
if(Control == null)
{
mediaRouteButton = new MediaRouteButton(Context);
mediaRouter = MediaRouter.GetInstance(Context);
mediaRouteSelector = new MediaRouteSelector
.Builder()
.AddControlCategory(CastMediaControlIntent.CategoryForCast("0A6928D1"))
.Build();
mediaRouterCallback = new CustomMediaRouterCallBack();
mediaRouter.AddCallback(mediaRouteSelector, mediaRouterCallback, MediaRouter.CallbackFlagPerformActiveScan);
mediaRouteButton.RouteSelector = mediaRouteSelector;
linearLayout = new LinearLayout(Context);
linearLayout.AddView(mediaRouteButton);
SetNativeControl(linearLayout);
}
}
}
public class CustomMediaRouterCallBack : MediaRouter.Callback
{
CastDevice castDevice;
public override void OnRouteSelected(MediaRouter router, MediaRouter.RouteInfo route)
{
castDevice = CastDevice.GetFromBundle(route.Extras);
System.Diagnostics.Debug.WriteLine("SELECTED; Connected to: " + route.Name);
}
public override void OnRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route)
{
System.Diagnostics.Debug.WriteLine("UNSELECTED");
castDevice = null;
}
}
}
}