private void OnCombinePickerClick(object sender, RoutedEventArgs e) { if (devicePicker == null) { devicePicker = new DevicePicker(); // add casting devicePicker.Filter.SupportedDeviceSelectors.Add(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); // add dial devicePicker.Filter.SupportedDeviceSelectors.Add(DialDevice.GetDeviceSelector("castingsample")); // add projection devicePicker.Filter.SupportedDeviceSelectors.Add(ProjectionManager.GetDeviceSelector()); devicePicker.DevicePickerDismissed += DevicePicker_DevicePickerDismissed; devicePicker.DeviceSelected += DevicePicker_DeviceSelected; devicePicker.DisconnectButtonClicked += DevicePicker_DisconnectButtonClicked; } player.Pause(); // 從按下的 button 出現 picker 内容 Button btn = sender as Button; GeneralTransform transform = btn.TransformToVisual(Window.Current.Content as UIElement); Point pt = transform.TransformPoint(new Point(0, 0)); devicePicker.Show(new Rect(pt.X, pt.Y, btn.ActualWidth, btn.ActualHeight), Windows.UI.Popups.Placement.Above); }
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); } }); }
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 { } }); }
// </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; }); }
public Scenario3() { this.InitializeComponent(); //Create our watcher and have it find casting devices capable of video casting watcher = DeviceInformation.CreateWatcher(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); //Register for watcher events watcher.Added += Watcher_Added; watcher.Removed += Watcher_Removed; watcher.Stopped += Watcher_Stopped; watcher.EnumerationCompleted += Watcher_EnumerationCompleted; }
// </SnippetDeclareDeviceWatcher> // <SnippetStartWatcherButtonClick> private void startWatcherButton_Click(object sender, RoutedEventArgs e) { startWatcherButton.IsEnabled = false; watcherProgressRing.IsActive = true; castingDevicesListBox.Items.Clear(); //Create our watcher and have it find casting devices capable of video casting deviceWatcher = DeviceInformation.CreateWatcher(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); //Register for watcher events deviceWatcher.Added += DeviceWatcher_Added; deviceWatcher.Removed += DeviceWatcher_Removed; deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; deviceWatcher.Stopped += DeviceWatcher_Stopped; }
public CastTrackDialog() { // Create the UI InitializeComponent(); // Create a device watcher for audio and video devices _deviceWatcher = DeviceInformation.CreateWatcher(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Audio | CastingPlaybackTypes.Video)); // Bind the events _deviceWatcher.Added += OnDeviceAdd; _deviceWatcher.Removed += OnDeviceRemove; _deviceWatcher.EnumerationCompleted += OnDeviceWatcherComplete; _deviceWatcher.Stopped += OnDeviceWatcherStop; // Start looking for devices _deviceWatcher.Start(); LoadingRing.Visibility = Visibility.Visible; }
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); } }); }
private async void castButton_Click(object sender, RoutedEventArgs e) { player.Pause(); //Retrieve the location of the casting button GeneralTransform transform = castButton.TransformToVisual(Window.Current.Content as UIElement); Point pt = transform.TransformPoint(new Point(0, 0)); //Add the DIAL Filter, so that the application only shows DIAL devices that have the application installed or advertise that they can install them. picker.Filter.SupportedDeviceSelectors.Add(DialDevice.GetDeviceSelector(this.dial_appname_textbox.Text)); //Add the CAST API Filter, so that the application only shows Miracast, Bluetooth, DLNA devices that can render the video picker.Filter.SupportedDeviceSelectors.Add(await CastingDevice.GetDeviceSelectorFromCastingSourceAsync(player.GetAsCastingSource())); //RequiredDeviceProperties.AddProps(picker.RequestedProperties); //if (activeDevice != null) // //Update the display status for the previously selected device. // picker.SetDisplayStatus(activeDevice, "Connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton); //Show the picker above our Show Device Picker button picker.Show(new Rect(pt.X, pt.Y, castButton.ActualWidth, castButton.ActualHeight), Windows.UI.Popups.Placement.Above); rootPage.NotifyUser("Show Device Picker Button Clicked", NotifyType.StatusMessage); }
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); }