Streaming media to devices using Play To (HTML)
[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]
Play To enables users to easily stream audio, video, or images from their computer to networked devices.
Introduction
You can use Play To to enable users to easily stream audio, video, or images from their computer to devices in their home network. For example, a user who is watching a video in your application can stream that video to their TV for everyone in the room to view.
Play To streams audio, video, or images to a certified Play To receiver.
Play To is part of the Devices charm. For applications that contain audio, video, or images, the user opens the Devices charm and is presented with devices from their home network that they can stream the media content to.
Using Play To in your application
You can use Play To to stream the audio or video in your application, as well as images, by implementing the Play To contract. To implement the Play To contract in your application, register for the sourceRequested event. To register for the sourceRequested event, get a reference to the current PlayToManager by calling the getForCurrentView method. You can then call addEventHandler on the PlayToManager to associate your event handler with the sourceRequested event. In your event handler, pass the media element from your application to the setSource method of the PlayToSourceRequestedEventArgs object passed to the event handler as shown in the following example.
// Play To Contract
private Windows.Media.PlayTo.PlayToManager ptm =
Windows.Media.PlayTo.PlayToManager.GetForCurrentView();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ptm.SourceRequested += sourceRequestHandler;
}
private void sourceRequestHandler(
Windows.Media.PlayTo.PlayToManager sender,
Windows.Media.PlayTo.PlayToSourceRequestedEventArgs e)
{
try
{
e.SourceRequest.SetSource(mediaElement.PlayToSource);
}
catch (Exception ex)
{
messageBlock.Text += "Exception encountered: " + ex.Message + "\n";
}
}
// Play To Contract
var ptm = Windows.Media.PlayTo.PlayToManager.getForCurrentView();
ptm.addEventListener("sourcerequested", sourceRequestHandler, false);
function sourceRequestHandler(e) {
try {
e.sourceRequest.setSource(mediaElement.msPlayToSource);
} catch (ex) {
id("messageDiv").innerHTML += "Exception encountered: " + ex.message + "<br/>";
}
}
' 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
When a user selects a Play To target from the Devices charm, Play To raises the sourceRequested event. Play To then streams the media element passed to the setSource method to the target device selected by the user.
If Play To is streaming media from your application to a target device, Play To will continue to stream the media from your application to the target device even if your application is moved to the background while another application is active. For more information, see Launching, resuming, and multitasking.
Play To Default Behavior
By default, Play To is enabled for all audio and video elements on an application page. You can disable this default behavior in your app, or you can identify which audio or video elements are not available to the default Play To behavior.
To disable the default Play To behavior in your application, set the defaultSourceSelection property of the PlayToManager object to false.
var ptm = Windows.Media.PlayTo.PlayToManager.getForCurrentView();
ptm.defaultSourceSelection = false;
To disable the default Play To behavior for a specific audio or video element, include the x-ms-playToDisabled property in the element markup.
<video src="https://sample.microsoft.com/video.mp4" x-ms-playToDisabled />
You can also disable the default Play To behavior for an element in JavaScript by setting the msPlayToDisabled property to true.
If you have more than one audio or video element on a page and you want to identify an element as the first element for Play To to stream you can include the x-ms-playToPrimary property in the element markup.
<video src="https://sample.microsoft.com/showvideo.mp4" x-ms-playToPrimary />
You can also identify the first element for Play To to stream in JavaScript by setting the msPlayToPrimary property to true.
Guidelines for using Play To in an application
If your application enables users to view video or images, or to listen to audio, be sure to include Play To functionality in your application to also enable the user to stream the media to a remote device.
Ensure that you enable the Devices charm in your application wherever you have audio, video, or image content available to the user.
Ensure that users can continue to navigate through your application once they have begun streaming media to a remote device by using Play To. To keep media streaming, you must ensure that the media element in your application remains within scope.
Because Play To is part of the Windows UI, do not require users to navigate to a specific page or screen of your application in order to use Play To.
Related topics
Quickstart: Using Play To in applications
Quickstart: Streaming a slide show using Play To
Samples