Quickstart: Streaming a slide show using Play To (XAML)
You can use Play To to enable users to easily stream audio, video, or images from their computer to devices in their home network. This topic shows you how to use Play To in a Windows Store app, to enable users to stream images as a slide show to a target device.
Objective: Use Play To to stream images as a slide show to a target device.
Prerequisites
Microsoft Visual Studio
Instructions
1. Create a new project and enable access to Pictures
- Open Visual Studio and select New Project from the File menu. In the Visual C# or Visual Basic section, select Application. Name the application PlayToSlideShow and click OK.
- Open the Package.appxmanifest file and select the Capabilities tab. Select the Pictures Library capability to enable your application to access the Pictures folder on a computer. Close and save the manifest file.
2. Add HTML UI
Open the MainPage.xaml file add the following to the default <Grid>. The UI contains a StackPanel that is used to display the images and a TextBlock that is used to display status messages. The UI also contains a TextBlock that tells the user how to begin streaming using Play To and a Button to enable the user to disconnect when streaming. These two elements are hidden and made visible depending on whether the slide show is streaming or not.
<StackPanel Orientation="Vertical"> <StackPanel x:Name="SlideShowPanel" Height="600" Orientation="Horizontal" /> <TextBlock x:Name="MessageBlock" FontSize="14" >Slideshow disconnected</TextBlock> <Button x:Name="DisconnectButton" Width="600" Height="60" Visibility="Collapsed" FontSize="14"> <StackPanel Orientation="Horizontal"> <TextBlock>Connected to</TextBlock> <Image x:Name="IconImage" Width="30" /> <TextBlock x:Name="DeviceBlock" /> </StackPanel> </Button> <TextBlock x:Name="InstructionsBlock" FontSize="14" >Swipe from the right edge of the screen, select "Devices", and select a device to stream the slide show to.</TextBlock> </StackPanel>
3. Add initialization code
The code in this step initializes the PlayToManager and then begins the slide show.
Open the MainPage.xaml.cs or MainPage.xaml.vb file and add the following code in place of the default OnNavigatedTo method.
4. Add code to get and display the images as a slide show
This sample displays images as a slide show by using the images in the root folder of Pictures. This is accomplished by first obtaining the list of images from Pictures, and then creating <image> objects to cycle through the list.
When streaming the images using Play To, the code for this slide show app makes use of the ability to buffer the next media item using Play To. This is optional, but it is useful for scenarios where it takes additional time to obtain the next media item to stream. By buffering the media, you can stream it immediately after the current media item has finished and avoid a delay between media items.
To buffer the next media item, we set the Play To source of the next item to the Next property of the current item. When the current item completes, we call the PlayNext method of the current item to stream the next media source to the target device.
When the slide show is only playing locally, the code uses a timeout to move to the next image in the list. When the slide show is streaming to a Play To receiver, the code still uses a timeout to move to the next image, but also responds when the Play To receiver pauses the slide show, moves to the next image before the timeout expires, or is disconnected. This is accomplished using the Statechanged event of the Play To source referenced by the PlayToSource property of an image object. In the Statechanged event, the code examines the CurrentState and PreviousState properties of the arguments passed to the event. The different states, along with the number that identifies the index of the image that raised the Statechanged event tell us how to respond as shown in the following table.
| CurrentState | Action to take |
|---|---|
| disconnected |
If the index of the image that raised the event is the same as the index of the image that is currently being displayed, then the Play To source was disconnected while an image was being displayed. This means that the Play To receiver is no longer connected and we start the slide show locally using the most recent image index. Otherwise, a state of Disconnected simply indicates that the slide show has finished displaying the image that raised the event and we can clean up the image object that is no longer required. |
| connected |
If the previous state is Disconnected, then the image that raised the event has just been connected to the Play To receiver. At this point, we get the next image so that it is loaded while the current image is displayed. If the previous state is Rendering, then the user has paused the slide show on the Play To receiver and we clear the current timeout until the user restarts the show. |
| rendering | If the previous state is Connected, then the Play To receiver has un-paused the slide show and we can start the show again. |
In the MainPage.xaml.cs or MainPage.xaml.cs file, add the following code after the code from the previous step.
5. Add Play To code
The code in this step implements the Play To contract. It gets a reference to the PlayToManager for the current application and associates the event handler for the SourceRequested and SourceSelected events.
Because image objects are created and destroyed for each image of the slide show, we use a temporary image object that is never destroyed in the SourceRequested event. This is because we do not know if the timeout will expire before the user selects a Play To receiver. If that occurred, then the current image would be destroyed and we would pass a null reference to Play To. Instead, we pass Play To a reference to image object that is never destroyed and update the image source to the currently displayed image once the user selects a Play To receiver. We know that has occurred when the state of the image changes to Connected.
In the MainPage.xaml.cs or MainPage.xaml.vb file, add the following code after the code from the previous step.
6. Create a Play To target (optional)
To run the application, you will need a target device to which Play To can stream media. If you do not have a certified Play To receiver, you can use Windows Media Player as a target device. To use Windows Media Player as a target device, your computer must be connected to a private network. Windows Media Player must be running on a different computer than your Play To source app.
- Start Windows Media Player.
- Expand the Stream menu and enable the Allow remote control of my Player... option. Leave Windows Media Player open; it must be running to be available as a Play To target.
- Open the Devices and Printers control panel. Click Add devices and printers. In the Add devices and printers wizard, in the Choose a device or printer to add to this PC window, locate the Digital media renderer for your PC. This is the Windows Media Player for your PC. Select it and click Next. When the wizard finishes, you will see your instance of Windows Media Player in the list of Multimedia Devices.
7. Run the app
- In Visual Studio, press F5 (debug) to run the app. You can select any of the media buttons to play or view the first media item in the different media libraries. While the media is playing, open the Devices charm and select your Play To target to stream the media to the target device.
Summary
In this quickstart, you added Play To capability to an application that displays images as a slide show on a target device. The Play To capability enables users to stream the content from the application to a certified Play To receiver on their network.
Related topics
- Streaming media to devices using Play To
- Samples
- Play To sample
- PlayToReceiver sample
- Media Server sample