Skip to content

Commit f003824

Browse files
committed
Updating snippet paths to point to ported snippets
1 parent 4cf759e commit f003824

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

hub/apps/develop/media-playback/media-casting.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ The simplest way to cast media from a WinUI app is to use the built-in casting c
1818

1919
In your app's XAML file, add a **MediaPlayerElement** and set [**AreTransportControlsEnabled**](/uwp/api/windows.ui.xaml.controls.mediaelement.aretransportcontrolsenabled) to true.
2020

21-
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml" id="SnippetMediaElement":::
21+
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml" id="SnippetMediaElement":::
2222

2323
Add a button to let the user initiate picking a file.
2424

25-
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml" id="SnippetOpenButton":::
25+
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml" id="SnippetOpenButton":::
2626

2727
In the [**Click**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.primitives.buttonbase.click) event handler for the button, create a new instance of the [**FileOpenPicker**](/uwp/api/Windows.Storage.Pickers.FileOpenPicker), add video file types to the [**FileTypeFilter**](/uwp/api/windows.storage.pickers.fileopenpicker.filetypefilter) collection, and set the starting location to the user's videos library.
2828

2929
Call [**PickSingleFileAsync**](/windows/windows-app-sdk/api/winrt/microsoft.windows.storage.pickers.fileopenpicker.picksinglefileasync) to launch the file picker dialog. When this method returns, the result is a [**StorageFile**](/uwp/api/Windows.Storage.StorageFile) object representing the video file. Check to make sure the file isn't null, which it will be if the user cancels the picking operation. Call the file's [**OpenAsync**](/uwp/api/windows.storage.storagefile.openasync) method to get an [**IRandomAccessStream**](/uwp/api/Windows.Storage.Streams.IRandomAccessStream) for the file. Finally, create a new **MediaSource** object from the selected file by calling [**CreateFromStorageFile**](/uwp/api/windows.media.core.mediasource.createfromstoragefile) and assign it to the **MediaPlayerElement** object's [**Source**](/uwp/api/windows.ui.xaml.controls.mediaplayerelement.source) property to make the video file the video source for the control.
3030

31-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetOpenButtonClick":::
31+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetOpenButtonClick":::
3232

3333
Once the video is loaded in the **MediaPlayerElement**, the user can simply press the casting button on the transport controls to launch a built-in dialog that allows them to choose a device to which the loaded media will be cast.
3434

@@ -39,30 +39,30 @@ Once the video is loaded in the **MediaPlayerElement**, the user can simply pres
3939

4040
A second way to cast media to a device is to use the [**CastingDevicePicker**](/uwp/api/Windows.Media.Casting.CastingDevicePicker). First, declare a member variable for the **Windows.Media.Casting.CastingDevicePicker** object.
4141

42-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetDeclareCastingPicker":::
42+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetDeclareCastingPicker":::
4343

4444
When your window is initialized, create a new instance of the casting picker and set the [**Filter**](/uwp/api/windows.media.casting.castingdevicepicker.filter) to [**SupportsVideo**](/uwp/api/Windows.Media.Casting.CastingDevicePickerFilter) property to indicate that the casting devices listed by the picker should support video. Register a handler for the [**CastingDeviceSelected**](/uwp/api/windows.media.casting.castingdevicepicker.castingdeviceselected) event, which is raised when the user picks a device for casting.
4545

46-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetInitCastingPicker":::
46+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetInitCastingPicker":::
4747

4848
In your XAML file, add a button to allow the user to launch the picker.
4949

50-
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml" id="SnippetCastPickerButton":::
50+
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml" id="SnippetCastPickerButton":::
5151

5252
In the **Click** event handler for the button, call [**TransformToVisual**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.uielement.transformtovisual) to get the transform of a UI element relative to another element. In this example, the transform is the position of the cast picker button relative to the visual root of the application window. Call the [**Show**](/uwp/api/windows.media.casting.castingdevicepicker.show) method of the [**CastingDevicePicker**](/uwp/api/Windows.Media.Casting.CastingDevicePicker) object to launch the casting picker dialog. Specify the location and dimensions of the cast picker button so that the system can make the dialog fly out from the button that the user pressed.
5353

54-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetCastPickerButtonClick":::
54+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetCastPickerButtonClick":::
5555

5656
In the **CastingDeviceSelected** event handler, call the [**CreateCastingConnection**](/uwp/api/windows.media.casting.castingdevice.createcastingconnection) method of the [**SelectedCastingDevice**](/uwp/api/windows.media.casting.castingdeviceselectedeventargs.selectedcastingdevice) property of the event args, which represents the casting device selected by the user. Register handlers for the [**ErrorOccurred**](/uwp/api/windows.media.casting.castingconnection.erroroccurred) and [**StateChanged**](/uwp/api/windows.media.casting.castingconnection.statechanged) events. Finally, call [**RequestStartCastingAsync**](/uwp/api/windows.media.casting.castingconnection.requeststartcastingasync) to begin casting, passing in the result to the **MediaPlayerElement** control's **MediaPlayer** object's [**GetAsCastingSource**](/uwp/api/windows.ui.xaml.controls.mediaelement.getascastingsource) method to specify that the media to be cast is the content of the **MediaPlayer** associated with the **MediaPlayerElement**.
5757

5858
> [!NOTE]
5959
> The casting connection must be initiated on the UI thread. Since the **CastingDeviceSelected** is not called on the UI thread, you must place these calls inside a call to [**CoreDispatcher.RunAsync**](/uwp/api/windows.ui.core.coredispatcher.runasync) which causes them to be called on the UI thread.
6060
61-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetCastingDeviceSelected":::
61+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetCastingDeviceSelected":::
6262

6363
In the **ErrorOccurred** and **StateChanged** event handlers, you should update your UI to inform the user of the current casting status. These events are discussed in detail in the following section on creating a custom casting device picker.
6464

65-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetEmptyStateHandlers":::
65+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetEmptyStateHandlers":::
6666

6767
## Media casting with a custom device picker
6868

@@ -75,45 +75,45 @@ Add the following controls to your XAML page to implement the rudimentary UI for
7575
- A [**ListBox**](/uwp/api/Windows.UI.Xaml.Controls.ListBox) to list the discovered casting devices. Define an [**ItemTemplate**](/uwp/api/windows.ui.xaml.controls.itemscontrol.itemtemplate) for the control so that we can assign the casting device objects directly to the control and still display the [**FriendlyName**](/uwp/api/windows.media.casting.castingdevice.friendlyname) property.
7676
- A button to allow the user to disconnect the casting device.
7777

78-
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml" id="SnippetCustomPickerXAML":::
78+
:::code language="xml" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml" id="SnippetCustomPickerXAML":::
7979

8080
In your code behind, declare member variables for the [**DeviceWatcher**](/uwp/api/Windows.Devices.Enumeration.DeviceWatcher) and the [**CastingConnection**](/uwp/api/Windows.Media.Casting.CastingConnection).
8181

82-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetDeclareDeviceWatcher":::
82+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetDeclareDeviceWatcher":::
8383

8484
In the **Click** handler for the *startWatcherButton*, first update the UI by disabling the button and making the progress ring active while device enumeration is ongoing. Clear the list box of casting devices.
8585

8686
Next, create a device watcher by calling [**DeviceInformation.CreateWatcher**](/uwp/api/windows.devices.enumeration.deviceinformation.createwatcher). This method can be used to watch for many different types of devices. Specify that you want to watch for devices that support video casting by using the device selector string returned by [**CastingDevice.GetDeviceSelector**](/uwp/api/windows.media.casting.castingdevice.getdeviceselector).
8787

8888
Finally, register event handlers for the [**Added**](/uwp/api/windows.devices.enumeration.devicewatcher.added), [**Removed**](/uwp/api/windows.devices.enumeration.devicewatcher.removed), [**EnumerationCompleted**](/uwp/api/windows.devices.enumeration.devicewatcher.enumerationcompleted), and [**Stopped**](/uwp/api/windows.devices.enumeration.devicewatcher.stopped) events.
8989

90-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetStartWatcherButtonClick":::
90+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetStartWatcherButtonClick":::
9191

9292
The **Added** event is raised when a new device is discovered by the watcher. In the handler for this event, create a new [**CastingDevice**](/uwp/api/Windows.Media.Casting.CastingDevice) object by calling [**CastingDevice.FromIdAsync**](/uwp/api/windows.media.casting.castingdevice.fromidasync) and passing in the ID of the discovered casting device, which is contained in the **DeviceInformation** object passed into the handler.
9393

9494
Add the **CastingDevice** to the casting device **ListBox** so that the user can select it. Because of the [**ItemTemplate**](/uwp/api/windows.ui.xaml.controls.itemscontrol.itemtemplate) defined in the XAML, the [**FriendlyName**](/uwp/api/windows.media.casting.castingdevice.friendlyname) property will be used as the item text for in the list box. Because this event handler is not called on the UI thread, you must update the UI from within a call to [**CoreDispatcher.RunAsync**](/uwp/api/windows.ui.core.coredispatcher.runasync).
9595

96-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetWatcherAdded":::
96+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetWatcherAdded":::
9797

9898
The **Removed** event is raised when the watcher detects that a casting device is no longer present. Compare the ID property of the **Added** object passed into the handler to the ID of each **Added** in the list box's [**Items**](/uwp/api/windows.ui.xaml.controls.itemscontrol.items) collection. If the ID matches, remove that object from the collection. Again, because the UI is being updated, this call must be made from within a **RunAsync** call.
9999

100-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetWatcherRemoved":::
100+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetWatcherRemoved":::
101101

102102
The **EnumerationCompleted** event is raised when the watcher has finished detecting devices. In the handler for this event, update the UI to let the user know that device enumeration has completed and stop the device watcher by calling [**Stop**](/uwp/api/windows.devices.enumeration.devicewatcher.stop).
103103

104-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetWatcherEnumerationCompleted":::
104+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetWatcherEnumerationCompleted":::
105105

106106
The Stopped event is raised when the device watcher has finished stopping. In the handler for this event, stop the [**ProgressRing**](/uwp/api/Windows.UI.Xaml.Controls.ProgressRing) control and reenable the *startWatcherButton* so that the user can restart the device enumeration process.
107107

108-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetWatcherStopped":::
108+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetWatcherStopped":::
109109

110110
When the user selects one of the casting devices from the list box, the [**SelectionChanged**](/uwp/api/windows.ui.xaml.controls.primitives.selector.selectionchanged) event is raised. It is within this handler that the casting connection will be created and casting will be started.
111111

112112
First, make sure the device watcher is stopped so that device enumeration doesn't interfere with media casting. Create a casting connection by calling [**CreateCastingConnection**](/uwp/api/windows.media.casting.castingdevice.createcastingconnection) on the **CastingDevice** object selected by the user. Add event handlers for the [**StateChanged**](/uwp/api/windows.media.casting.castingconnection.statechanged) and [**ErrorOccurred**](/uwp/api/windows.media.casting.castingconnection.erroroccurred) events.
113113

114114
Start media casting by calling [**RequestStartCastingAsync**](/uwp/api/windows.media.casting.castingconnection.requeststartcastingasync), passing in the casting source returned by calling the **MediaPlayer** method [**GetAsCastingSource**](/uwp/api/windows.ui.xaml.controls.mediaelement.getascastingsource). Finally, make the disconnect button visible to allow the user to stop media casting.
115115

116-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetSelectionChanged":::
116+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetSelectionChanged":::
117117

118118
In the state changed handler, the action you take depends on the new state of the casting connection:
119119

@@ -122,15 +122,15 @@ In the state changed handler, the action you take depends on the new state of th
122122
- If the state is **Connecting**, make the **ProgressRing** control active and hide the disconnect button.
123123
- If the state is **Disconnecting**, make the **ProgressRing** control active and hide the disconnect button.
124124

125-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetStateChanged":::
125+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetStateChanged":::
126126

127127
In the handler for the **ErrorOccurred** event, update your UI to let the user know that a casting error occurred and unselect the current **CastingDevice** object in the list box.
128128

129-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetErrorOccurred":::
129+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetErrorOccurred":::
130130

131131
Finally, implement the handler for the disconnect button. Stop media casting and disconnect from the casting device by calling the **CastingConnection** object's [**DisconnectAsync**](/uwp/api/windows.media.casting.castingconnection.disconnectasync) method. This call must be dispatched to the UI thread by calling [**CoreDispatcher.RunAsync**](/uwp/api/windows.ui.core.coredispatcher.runasync).
132132

133-
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MainWindow.xaml.cs" id="SnippetDisconnectButton":::
133+
:::code language="csharp" source="~/../snippets-windows/winappsdk/audio-video-camera/media-casting-winui/cs/MediaCasting_WinUI/MainWindow.xaml.cs" id="SnippetDisconnectButton":::
134134

135135

136136

0 commit comments

Comments
 (0)