-
Notifications
You must be signed in to change notification settings - Fork 495
/
MainPage.xaml.vb
186 lines (144 loc) · 6.83 KB
/
MainPage.xaml.vb
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
' The Blank Page item template is documented at http:'go.microsoft.com/fwlink/?LinkId=234238
''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
Inherits Page
''' <summary>
''' Invoked when this page is about to be displayed in a Frame.
''' </summary>
''' <param name="e">Event data that describes how this page was reached. The Parameter
''' property is typically used to configure the page.</param>
Private mediaElement As Windows.UI.Xaml.Controls.MediaElement
Private messageBlock As Windows.UI.Xaml.Controls.TextBlock
' <SnippetSourceRequested>
' Play To Contract
Private ptm As Windows.Media.PlayTo.PlayToManager =
Windows.Media.PlayTo.PlayToManager.GetForCurrentView()
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
AddHandler ptm.SourceRequested, AddressOf sourceRequestHandler
End Sub
Private Sub sourceRequestHandler(
sender As Windows.Media.PlayTo.PlayToManager,
e As Windows.Media.PlayTo.PlayToSourceRequestedEventArgs)
Try
e.SourceRequest.SetSource(mediaElement.PlayToSource)
Catch ex As Exception
messageBlock.Text &= "Exception encountered: " & ex.Message & vbCrLf
End Try
End Sub
' </SnippetSourceRequested>
End Class
' exclude from the running application
Class exclude1
Inherits Page
Private mediaElement As Windows.UI.Xaml.Controls.MediaElement
Private messageBlock As Windows.UI.Xaml.Controls.TextBlock
Private playToDeviceIconImage As Windows.UI.Xaml.Controls.Image
Private playToDeviceFriendlyNameBlock As Windows.UI.Xaml.Controls.TextBlock
' <SnippetPlayToEvents>
Private ptm As Windows.Media.PlayTo.PlayToManager =
Windows.Media.PlayTo.PlayToManager.GetForCurrentView()
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
AddHandler ptm.SourceRequested, AddressOf sourceRequestHandlerWithEvents
AddHandler ptm.SourceSelected, AddressOf sourceSelectedHandler
End Sub
Private Sub sourceRequestHandlerWithEvents(
sender As Windows.Media.PlayTo.PlayToManager,
e As Windows.Media.PlayTo.PlayToSourceRequestedEventArgs)
Try
Dim controller = mediaElement.PlayToSource
AddHandler controller.Connection.Error, AddressOf playToConnectionError
AddHandler controller.Connection.StateChanged, AddressOf playToConnectionStageChanged
AddHandler controller.Connection.Transferred, AddressOf playToConnectionTransferred
e.SourceRequest.SetSource(controller)
Catch ex As Exception
messageBlock.Text &= "Exception encountered: " & ex.Message & vbCrLf
End Try
End Sub
' Called when the user selects a Play To device to stream to.
Private Sub sourceSelectedHandler(
sender As Windows.Media.PlayTo.PlayToManager,
e As Windows.Media.PlayTo.PlayToSourceSelectedEventArgs)
If mediaElement.Name = "iplayer" Then
If Not e.SupportsImage Then
messageBlock.Text &= e.FriendlyName & " does not support streaming images. " &
"Please select a different device."
Return
End If
End If
If mediaElement.Name = "vplayer" Then
If Not e.SupportsVideo Then
messageBlock.Text &= e.FriendlyName & " does not support streaming video. " &
"Please select a different device."
Return
End If
End If
If mediaElement.Name = "aplayer" Then
If Not e.SupportsAudio Then
messageBlock.Text &= e.FriendlyName & " does not support streaming audio. " &
"Please select a different device."
Return
End If
End If
Dim iconStream = e.Icon
Dim iconBitmap = New Windows.UI.Xaml.Media.Imaging.BitmapImage()
iconBitmap.SetSource(iconStream)
playToDeviceIconImage.Source = iconBitmap
playToDeviceFriendlyNameBlock.Text = e.FriendlyName
End Sub
Private Sub playToConnectionError(
connection As Windows.Media.PlayTo.PlayToConnection,
e As Windows.Media.PlayTo.PlayToConnectionErrorEventArgs)
If e.Code = Windows.Media.PlayTo.PlayToConnectionError.DeviceError OrElse
e.Code = Windows.Media.PlayTo.PlayToConnectionError.DeviceNotResponding Then
messageBlock.Text &= "Error occurred. Disconnecting." & vbCrLf
End If
messageBlock.Text &= "Error: Message = " & e.Message.ToString() & vbCrLf
End Sub
Private Sub playToConnectionStageChanged(
connection As Windows.Media.PlayTo.PlayToConnection,
e As Windows.Media.PlayTo.PlayToConnectionStateChangedEventArgs)
messageBlock.Text &= "StateChanged: PreviousState = " & e.PreviousState.ToString() & vbCrLf
messageBlock.Text &= "StateChanged: CurrentState = " & e.CurrentState.ToString() & vbCrLf
End Sub
Private Sub playToConnectionTransferred(
connection As Windows.Media.PlayTo.PlayToConnection,
e As Windows.Media.PlayTo.PlayToConnectionTransferredEventArgs)
messageBlock.Text &= "Transferred: PreviousSource = " & e.PreviousSource.ToString() & vbCrLf
messageBlock.Text &= "Transferred: CurrentSource = " & e.CurrentSource.ToString() & vbCrLf
End Sub
' </SnippetPlayToEvents>
End Class
' exclude from the running application
Class exclude2
Inherits Page
Private mediaElement As Windows.UI.Xaml.Controls.MediaElement
' <SnippetDeferral>
Private ptm As Windows.Media.PlayTo.PlayToManager =
Windows.Media.PlayTo.PlayToManager.GetForCurrentView()
Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
AddHandler ptm.SourceRequested, AddressOf sourceRequestHandlerDeferred
End Sub
Private Async Sub sourceRequestHandlerDeferred(
sender As Windows.Media.PlayTo.PlayToManager,
e As Windows.Media.PlayTo.PlayToSourceRequestedEventArgs)
Dim deferral = e.SourceRequest.GetDeferral()
' Async call to get source media
Dim element = Await getMediaElementAsync()
e.SourceRequest.SetSource(element.PlayToSource)
deferral.Complete()
End Sub
' </SnippetDeferral>
Function getMediaElementAsync() As System.Threading.Tasks.Task(Of Windows.UI.Xaml.Controls.MediaElement)
Dim result = New System.Threading.Tasks.Task(Of Windows.UI.Xaml.Controls.MediaElement)(Function() mediaElement)
Return result
End Function
End Class
Class exclude3
' <SnippetShowPlayToUI>
Private Sub showPlayTo()
Windows.Media.PlayTo.PlayToManager.ShowPlayToUI()
End Sub
' </SnippetShowPlayToUI>
End Class