PlayToConnection
PlayToConnection
PlayToConnection
PlayToConnection
PlayToConnection
Class
Definition
Provides information about a Play To connection.
public : sealed class PlayToConnection : IPlayToConnection
struct winrt::Windows::Media::PlayTo::PlayToConnection : IPlayToConnection
public sealed class PlayToConnection : IPlayToConnection
Public NotInheritable Class PlayToConnection Implements IPlayToConnection
// This class does not provide a public constructor.
- Attributes
Windows 10 requirements
Device family |
Windows 10 (introduced v10.0.10240.0)
|
API contract |
Windows.Foundation.UniversalApiContract (introduced v1)
|
Examples
private Windows.Media.PlayTo.PlayToManager ptm =
Windows.Media.PlayTo.PlayToManager.GetForCurrentView();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ptm.SourceRequested += sourceRequestHandlerWithEvents;
ptm.SourceSelected += sourceSelectedHandler;
}
private void sourceRequestHandlerWithEvents(
Windows.Media.PlayTo.PlayToManager sender,
Windows.Media.PlayTo.PlayToSourceRequestedEventArgs e)
{
try
{
var controller = mediaElement.PlayToSource;
controller.Connection.Error += playToConnectionError;
controller.Connection.StateChanged += playToConnectionStageChanged;
controller.Connection.Transferred += playToConnectionTransferred;
e.SourceRequest.SetSource(controller);
}
catch (Exception ex)
{
messageBlock.Text += "Exception encountered: " + ex.Message + "\n";
}
}
// Called when the user selects a Play To device to stream to.
private void sourceSelectedHandler(
Windows.Media.PlayTo.PlayToManager sender,
Windows.Media.PlayTo.PlayToSourceSelectedEventArgs e)
{
if (mediaElement.Name == "iplayer") {
if (!e.SupportsImage) {
messageBlock.Text += e.FriendlyName + " does not support streaming images. " +
"Please select a different device.";
return;
}
}
if (mediaElement.Name == "vplayer") {
if (!e.SupportsVideo) {
messageBlock.Text += e.FriendlyName + " does not support streaming video. " +
"Please select a different device.";
return;
}
}
if (mediaElement.Name == "aplayer") {
if (!e.SupportsAudio) {
messageBlock.Text += e.FriendlyName + " does not support streaming audio. " +
"Please select a different device.";
return;
}
}
Windows.Storage.Streams.IRandomAccessStream iconStream = e.Icon;
Windows.UI.Xaml.Media.Imaging.BitmapImage iconBitmap =
new Windows.UI.Xaml.Media.Imaging.BitmapImage();
iconBitmap.SetSource(iconStream);
playToDeviceIconImage.Source = iconBitmap;
playToDeviceFriendlyNameBlock.Text = e.FriendlyName;
}
private void playToConnectionError(
Windows.Media.PlayTo.PlayToConnection connection,
Windows.Media.PlayTo.PlayToConnectionErrorEventArgs e)
{
if (e.Code == Windows.Media.PlayTo.PlayToConnectionError.DeviceError ||
e.Code == Windows.Media.PlayTo.PlayToConnectionError.DeviceNotResponding)
{
messageBlock.Text += "Error occurred. Disconnecting.\n";
}
messageBlock.Text += "Error: Message = " + e.Message.ToString() + "\n";
}
private void playToConnectionStageChanged(
Windows.Media.PlayTo.PlayToConnection connection,
Windows.Media.PlayTo.PlayToConnectionStateChangedEventArgs e)
{
messageBlock.Text += "StateChanged: PreviousState = " + e.PreviousState.ToString() + "\n";
messageBlock.Text += "StateChanged: CurrentState = " + e.CurrentState.ToString() + "\n";
}
private void playToConnectionTransferred(
Windows.Media.PlayTo.PlayToConnection connection,
Windows.Media.PlayTo.PlayToConnectionTransferredEventArgs e)
{
messageBlock.Text += "Transferred: PreviousSource = " + e.PreviousSource.ToString() + "\n";
messageBlock.Text += "Transferred: CurrentSource = " + e.CurrentSource.ToString() + "\n";
}
var ptm = Windows.Media.PlayTo.PlayToManager.getForCurrentView();
ptm.addEventListener("sourcerequested", sourceRequestHandlerWithEvents, false);
ptm.addEventListener("sourceselected", sourceSelectedHandler, false);
function sourceRequestHandlerWithEvents(e) {
try {
var controller = mediaElement.msPlayToSource;
controller.connection.addEventListener("error", playToConnectionError, false);
controller.connection.addEventListener("stateChanged", playToConnectionStageChanged, false);
controller.connection.addEventListener("transferred", playToConnectionTransferred, false);
e.sourceRequest.setSource(controller);
} catch (ex) {
id("messageDiv").innerHTML += "Exception encountered: " + ex.message + "<br/>";
}
}
// Called when the user selects a Play To device to stream to.
function sourceSelectedHandler(e) {
if (mediaElement.id == "iplayer") {
if (!e.supportsImage) {
id("messageDiv").innerHTML += e.friendlyName + " does not support streaming images. " +
"Please select a different device.";
return;
}
}
if (mediaElement.id == "vplayer") {
if (!e.supportsVideo) {
id("messageDiv").innerHTML += e.friendlyName + " does not support streaming video. " +
"Please select a different device.";
return;
}
}
if (mediaElement.id == "aplayer") {
if (!e.supportsAudio) {
id("messageDiv").innerHTML += e.friendlyName + " does not support streaming audio. " +
"Please select a different device.";
return;
}
}
var iconBlob = window.msWWA.createBlobFromRandomAccessStream(e.icon.contentType, e.icon);
id("playToDeviceIconImage").src = window.URL.createObjectURL(iconBlob, false);
id("playToDeviceFriendlyName").innerHTML = e.friendlyName;
id("playToStatusDiv").style.display = "block";
}
function playToConnectionError(e) {
if (e.code == Windows.Media.PlayTo.PlayToConnectionError.deviceError |
e.code == Windows.Media.PlayTo.PlayToConnectionError.deviceNotResponding) {
id("messageDiv").innerHTML += "Error occurred. Disconnecting.<br/>";
}
id("messageDiv").innerHTML += "Error: Message = " + e.message.toString() + "<br/>";
}
function playToConnectionStageChanged(e) {
id("messageDiv").innerHTML += "StateChanged: PreviousState = " + stateToString(e.previousState) + "<br/>";
id("messageDiv").innerHTML += "StateChanged: CurrentState = " + stateToString(e.currentState) + "<br/>";
}
function stateToString(state) {
switch (state) {
case Windows.Media.PlayTo.PlayToConnectionState.connected:
return "Connected";
case Windows.Media.PlayTo.PlayToConnectionState.disconnected:
return "Disconnected";
case Windows.Media.PlayTo.PlayToConnectionState.rendering:
return "Rendering";
}
}
function playToConnectionTransferred(e) {
id("messageDiv").innerHTML += "Transferred: PreviousSource = " + e.previousSource.toString() + "<br/>";
id("messageDiv").innerHTML += "Transferred: CurrentSource = " + e.currentSource.toString() + "<br/>";
}
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
Remarks
For an example of how to use Play To in an application, see Quickstart: Using Play To in applications (JavaScript) or PlayReady DRM.
Properties
State State State State State |
Gets the state of the Play To connection. |
Events
Error Error Error Error Error |
Occurs when an error is encountered for the Play To connection. |
StateChanged StateChanged StateChanged StateChanged StateChanged |
Occurs when the state of the Play To connection has changed. |
Transferred Transferred Transferred Transferred Transferred |
Occurs when the Play To connection is transferred to the next Play To source. |