Beispiel #1
0
        private async void DevicePicker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                DeviceInformation selectedDevice = args.SelectedDevice;

#if DEBUG
                // The args.SelectedCastingDevice is proxied from the picker process. The picker process is
                // dismissmed as soon as you break into the debugger. Creating a non-proxied version
                // allows debugging since the proxied version stops working once the picker is dismissed.
                selectedDevice = await DeviceInformation.CreateFromIdAsync(args.SelectedDevice.Id);
#endif

                if (await DialDevice.DeviceInfoSupportsDialAsync(selectedDevice))
                {
                    await SendDialParameter(sender, args);
                }
                else if (await CastingDevice.DeviceInfoSupportsCastingAsync(selectedDevice))
                {
                    await CastingVideoToScreen(sender, args);
                }
                else if (ProjectionManager.ProjectionDisplayAvailable)
                {
                    await ProjectioinViewToScreen(sender, args);
                }
            });
        }
Beispiel #2
0
        private async Task <bool> TryCastMediaElementAsync(DeviceInformation device)
        {
            bool castMediaElementSucceeded = false;

            rootPage.NotifyUser(string.Format("Checking to see if device {0} supports Miracast, Bluetooth, or DLNA", device.Name), NotifyType.StatusMessage);
            if (await CastingDevice.DeviceInfoSupportsCastingAsync(device))
            {
                CastingConnection connection = null;

                //Check to see whether we are casting to the same device
                if (activeDevice != null && device.Id == activeDevice.Id)
                {
                    connection = activeCastConnectionHandler as CastingConnection;
                }
                else // if not casting to the same device reset the active device related variables.
                {
                    activeDevice = null;
                    activeCastConnectionHandler = null;
                }

                // If we can re-use the existing connection
                if (connection == null || connection.State == CastingConnectionState.Disconnected || connection.State == CastingConnectionState.Disconnecting)
                {
                    CastingDevice castDevice = null;
                    activeDevice = null;

                    //Try to create a CastingDevice instannce. If it doesn't succeed, the selected device does not support playback of the video source.
                    rootPage.NotifyUser(string.Format("Attempting to resolve casting device for '{0}'", device.Name), NotifyType.StatusMessage);
                    try { castDevice = await CastingDevice.FromIdAsync(device.Id); } catch { }

                    if (castDevice == null)
                    {
                        //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL.
                        rootPage.NotifyUser(string.Format("'{0}' does not support playback of this media", device.Name), NotifyType.StatusMessage);
                    }
                    else
                    {
                        //Create a casting conneciton from our selected casting device
                        rootPage.NotifyUser(string.Format("Creating connection for '{0}'", device.Name), NotifyType.StatusMessage);
                        connection = castDevice.CreateCastingConnection();

                        //Hook up the casting events
                        connection.ErrorOccurred += Connection_ErrorOccurred;
                        connection.StateChanged  += Connection_StateChanged;
                    }

                    //Cast the content loaded in the media element to the selected casting device
                    rootPage.NotifyUser(string.Format("Casting to '{0}'", device.Name), NotifyType.StatusMessage);

                    CastingSource source = null;
                    // Get the casting source
                    try { source = player.GetAsCastingSource(); } catch { }

                    if (source == null)
                    {
                        rootPage.NotifyUser(string.Format("Failed to get casting source for video '{0}'", video.Title), NotifyType.ErrorMessage);
                    }
                    else
                    {
                        CastingConnectionErrorStatus status = await connection.RequestStartCastingAsync(source);

                        if (status == CastingConnectionErrorStatus.Succeeded)
                        {
                            //Remember the device to which casting succeeded
                            activeDevice = device;
                            //Remember the current active connection.
                            activeCastConnectionHandler = connection;
                            castMediaElementSucceeded   = true;
                        }
                        else
                        {
                            rootPage.NotifyUser(string.Format("Failed to cast to '{0}'", device.Name), NotifyType.ErrorMessage);
                        }
                    }
                }
            }
            else
            {
                rootPage.NotifyUser(string.Format("'{0}' does not support Miracast, Bluetooth, or DLNA", device.Name), NotifyType.StatusMessage);
            }
            return(castMediaElementSucceeded);
        }