示例#1
0
        private async Task CastingVideoToScreen(DevicePicker sender, DeviceSelectedEventArgs args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                activeDevice         = args.SelectedDevice;
                CastingDevice device = await CastingDevice.FromIdAsync(args.SelectedDevice.Id);
                castingConnection    = device.CreateCastingConnection();

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

                // Get the casting source from the MediaElement
                CastingSource source = null;

                try
                {
                    // Get the casting source from the Media Element
                    source = player.GetAsCastingSource();

                    // Start Casting
                    CastingConnectionErrorStatus status = await castingConnection.RequestStartCastingAsync(source);

                    if (status == CastingConnectionErrorStatus.Succeeded)
                    {
                        player.Play();
                    }
                }
                catch
                {
                }
            });
        }
示例#2
0
        // </SnippetStartWatcherButtonClick>

        // <SnippetWatcherAdded>
        private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                //Add each discovered device to our listbox
                CastingDevice addedDevice = await CastingDevice.FromIdAsync(args.Id);
                castingDevicesListBox.Items.Add(addedDevice);
            });
        }
 private async void Watcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
 {
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
     {
         //Remove any removed devices from our listbox
         CastingDevice addedDevice = await CastingDevice.FromIdAsync(args.Id);
         castingDevicesList.Items.Remove(addedDevice);
     });
 }
        private async void OnDeviceAdd(DeviceWatcher sender, DeviceInformation args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                // Add each discovered device to the list
                var addedDevice = await CastingDevice.FromIdAsync(args.Id);
                CastingDevices.Add(addedDevice);

                NoDevicesTextBlock.Visibility = CastingDevices.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
            });
        }
        private async void Picker_CastingDeviceSelected(CastingDevicePicker sender, CastingDeviceSelectedEventArgs args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                // 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.
                CastingDevice selectedDevice = await CastingDevice.FromIdAsync(args.SelectedCastingDevice.Id);

                //Create a casting conneciton from our selected casting device
                rootPage.NotifyUser(string.Format("Creating connection for '{0}'", selectedDevice.FriendlyName), NotifyType.StatusMessage);
                CastingConnection connection = selectedDevice.CreateCastingConnection();

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

                // Get the casting source from the MediaElement
                CastingSource source = null;

                try
                {
                    // Get the casting source from the Media Element
                    source = player.GetAsCastingSource();

                    // Start Casting
                    rootPage.NotifyUser(string.Format("Starting casting to '{0}'", selectedDevice.FriendlyName), NotifyType.StatusMessage);
                    CastingConnectionErrorStatus status = await connection.RequestStartCastingAsync(source);

                    if (status == CastingConnectionErrorStatus.Succeeded)
                    {
                        player.Play();
                        rootPage.NotifyUser(string.Format("Starting casting to '{0}'", selectedDevice.FriendlyName), NotifyType.StatusMessage);
                    }
                }
                catch
                {
                    rootPage.NotifyUser(string.Format("Failed to get casting source for video '{0}'", video.Title), NotifyType.ErrorMessage);
                }
            });
        }
示例#6
0
        private async Task <bool> TryCastMediaElementAsync(DeviceInformation device)
        {
            bool castMediaElementSucceeded = false;

            //Verify whether the selected device supports DLNA, Bluetooth, or Miracast.
            rootPage.NotifyUser(string.Format("Checking to see if device {0} supports Miracast, Bluetooth, or DLNA", device.Name), NotifyType.StatusMessage);

            //BUG: Takes too long. Workaround, just try to create the CastingDevice
            //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;
                        player.Play();
                    }
                    else
                    {
                        rootPage.NotifyUser(string.Format("Failed to cast to '{0}'", device.Name), NotifyType.ErrorMessage);
                    }
                }
                //}
            }
            return(castMediaElementSucceeded);
        }