private async void TransportControls_CastButtonClicked(object sender, EventArgs e) { //Pause Current Playback player.Pause(); rootPage.NotifyUser("Show Device Picker Button Clicked", NotifyType.StatusMessage); //Get the custom transport controls MediaTransportControlsWithCustomCastButton mtc = (MediaTransportControlsWithCustomCastButton)this.player.TransportControls; //Retrieve the location of the casting button GeneralTransform transform = mtc.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)); System.Diagnostics.Debug.WriteLine(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())); //picker.Filter.SupportedDeviceSelectors.Add(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); //picker.Filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{D0875FB4-2196-4c7a-A63D-E416ADDD60A1}\"" + " AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True"); //Add projection manager filter //picker.Filter.SupportedDeviceSelectors.Add(ProjectionManager.GetDeviceSelector()); //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, mtc.CastButton.ActualWidth, mtc.CastButton.ActualHeight), Windows.UI.Popups.Placement.Above); }
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 void Picker_DialDeviceSelected(DialDevicePicker sender, DialDeviceSelectedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. 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. DialDevice selectedDevice = await DialDevice.FromIdAsync(args.SelectedDialDevice.Id); // Set the status to connecting try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Connecting); } catch { } // Get the DeviceInformation instance for the the selected device DeviceInformation selectedDeviceInfo = await DeviceInformation.CreateFromIdAsync(selectedDevice.Id); //Get the DialApp object for the specific application on the selected device DialApp app = selectedDevice.GetDialApp(this.dial_appname_textbox.Text); if (app == null) { //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", selectedDeviceInfo.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage); try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { } } else { //Get the current application state DialAppStateDetails stateDetails = await app.GetAppStateAsync(); if (stateDetails.State == DialAppState.NetworkFailure) { // In case getting the application state failed because of a network failure rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage); try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { } } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); //Launch the application on the 1st screen device DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text); //Verify to see whether the application was launched if (result == DialAppLaunchResult.Launched) { rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage); //This is where you will need to add you application specific communication between your 1st and 2nd screen applications //... try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Connected); } catch { } } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { } } } } }); }
private async void Picker_DeviceSelected(DialDevicePicker sender, DialDeviceSelectedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { try { rootPage.NotifyUser(string.Format("Picker DeviceSelected event fired"), NotifyType.StatusMessage); // Set the status to connecting picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Connecting); rootPage.NotifyUser(string.Format("Resolving DialDevice'"), NotifyType.StatusMessage); //Get the DialApp object for the specific application on the selected device DialApp app = args.SelectedDialDevice.GetDialApp(this.dial_appname_textbox.Text); if (app == null) { //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. //rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", selectedDeviceInformation.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage); picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); //Launch the application on the 1st screen device DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text); //Verify to see whether the application was launched if (result == DialAppLaunchResult.Launched) { rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage); activeDialDevice = args.SelectedDialDevice; DeviceInformation selectedDeviceInformation = await DeviceInformation.CreateFromIdAsync(args.SelectedDialDevice.Id); activeDeviceInformation = selectedDeviceInformation; //This is where you will need to add you application specific communication between your 1st and 2nd screen applications //... //... //... picker.SetDisplayStatus(activeDialDevice, DialDeviceDisplayStatus.Connected); picker.Hide(); } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); } } } catch (Exception ex) { UnhandledExceptionPage.ShowUnhandledException(ex); } }); }
private async void Picker_DisconnectButtonClicked(DialDevicePicker sender, DialDisconnectButtonClickedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { try { rootPage.NotifyUser(string.Format("Picker DisconnectButtonClicked event fired for device '{0}'", activeDeviceInformation.Name), NotifyType.StatusMessage); // Get the DialDevice instance for the selected device DialDevice selectedDialDevice = await DialDevice.FromIdAsync(args.Device.Id); // Update the picker status picker.SetDisplayStatus(selectedDialDevice, DialDeviceDisplayStatus.Connecting); DialApp app = selectedDialDevice.GetDialApp(this.dial_appname_textbox.Text); //Get the current application state //DialAppStateDetails stateDetails = await app.GetAppStateAsync(); DialAppStopResult result = await app.StopAsync(); if (result == DialAppStopResult.Stopped) { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser("Application stopped successfully.", NotifyType.StatusMessage); picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Disconnected); activeDialDevice = null; activeDeviceInformation = null; picker.Hide(); } else { if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure) { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser(string.Format("Error occured trying to stop application. Status: '{0}'", result.ToString()), NotifyType.StatusMessage); picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Error); } else //in case of DialAppStopResult.OperationNotSupported, there is not much more you can do. You could implement your own // mechanism to stop the application on that device. { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser(string.Format("Stop is not supported by device: '{0}'", activeDeviceInformation.Name), NotifyType.ErrorMessage); activeDialDevice = null; activeDeviceInformation = null; } } } catch (Exception ex) { UnhandledExceptionPage.ShowUnhandledException(ex); } }); }
public Scenario06() { this.InitializeComponent(); rootPage = MainPage.Current; //Subscribe to player events player.MediaOpened += Player_MediaOpened; player.MediaFailed += Player_MediaFailed; player.CurrentStateChanged += Player_CurrentStateChanged; // Get an Azure hosted video AzureDataProvider dataProvider = new AzureDataProvider(); video = dataProvider.GetRandomVideo(); //Set the source on the player rootPage.NotifyUser(string.Format("Opening '{0}'", video.Title), NotifyType.StatusMessage); this.player.Source = video.VideoLink; //Configure the DIAL launch arguments for the current video this.dial_launch_args_textbox.Text = string.Format("v={0}&t=0&pairingCode=E4A8136D-BCD3-45F4-8E49-AE01E9A46B5F", video.Id); //Subscribe for the clicked event on the custom cast button ((MediaTransportControlsWithCustomCastButton)this.player.TransportControls).CastButtonClicked += TransportControls_CastButtonClicked; // Instantiate the Device Picker picker = new DevicePicker(); //Hook up device selected event picker.DeviceSelected += Picker_DeviceSelected; //Hook up device disconnected event picker.DisconnectButtonClicked += Picker_DisconnectButtonClicked; //Hook up device disconnected event picker.DevicePickerDismissed += Picker_DevicePickerDismissed; //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())); //picker.Filter.SupportedDeviceSelectors.Add(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); picker.Filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{D0875FB4-2196-4c7a-A63D-E416ADDD60A1}\"" + " AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True"); //Add projection manager filter picker.Filter.SupportedDeviceSelectors.Add(ProjectionManager.GetDeviceSelector()); pvb.ProjectionStopping += Pvb_ProjectionStopping; }
private async void DevicePicker_DisconnectButtonClicked(DevicePicker sender, DeviceDisconnectButtonClickedEventArgs args) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { DeviceInformation selectedDevice = args.Device; if (await DialDevice.DeviceInfoSupportsDialAsync(selectedDevice)) { await StopDialConnection(sender, selectedDevice); return; } await StopProjection(sender, selectedDevice); }); }
private async void Picker_DisconnectButtonClicked(DialDevicePicker sender, DialDisconnectButtonClickedEventArgs args) { // casting 必須在 UI Thread 下執行 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { try { // 取得被選擇的 dial device DialDevice selectedDialDevice = await DialDevice.FromIdAsync(args.Device.Id); // 更新 picker status picker.SetDisplayStatus(selectedDialDevice, DialDeviceDisplayStatus.Connecting); // 取得 dial app DialApp app = selectedDialDevice.GetDialApp(txtAppName.Text); // 請求斷綫 DialAppStopResult result = await app.StopAsync(); if (result == DialAppStopResult.Stopped) { picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Disconnected); activeDialDevice = null; activeDeviceInformation = null; picker.Hide(); tblMsg.Text += "Stoped, success"; } else { if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure) { // 如果失敗的話要記得多 retry 的機制 picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Error); tblMsg.Text += $"Stoped, {result}"; } else { // 如果設備沒有支援 Stop 機制,則直接清楚連綫就好 activeDialDevice = null; activeDeviceInformation = null; tblMsg.Text += "the device does not support Stop"; } } } catch (Exception ex) { tblMsg.Text += ex.Message; } }); }
private async void Picker_DisconnectButtonClicked(DialDevicePicker sender, DialDisconnectButtonClickedEventArgs args) { // casting 必須在 UI Thread 下執行 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { try { // 取得被選擇的 dial device DialDevice selectedDialDevice = await DialDevice.FromIdAsync(args.Device.Id); // 更新 picker status picker.SetDisplayStatus(selectedDialDevice, DialDeviceDisplayStatus.Connecting); // 取得 dial app DialApp app = selectedDialDevice.GetDialApp(txtAppName.Text); // 請求斷綫 DialAppStopResult result = await app.StopAsync(); if (result == DialAppStopResult.Stopped) { picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Disconnected); activeDialDevice = null; activeDeviceInformation = null; picker.Hide(); tblMsg.Text += "Stoped, success"; } else { if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure) { // 如果失敗的話要記得多 retry 的機制 picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Error); tblMsg.Text += $"Stoped, {result}"; } else { // 如果設備沒有支援 Stop 機制,則直接清楚連綫就好 activeDialDevice = null; activeDeviceInformation = null; tblMsg.Text += "the device does not support Stop"; } } } catch (Exception ex) { tblMsg.Text += ex.Message; } }); }
private async void Picker_DialDeviceSelected(DialDevicePicker sender, DialDeviceSelectedEventArgs args) { // casting 必須在 UI Thread 下執行 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { try { // 設定遠端設備現在要準備連綫 picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Connecting); // 取得遠端設備中支援指定 app name 的 App DialApp app = args.SelectedDialDevice.GetDialApp(txtAppName.Text); if (app == null) { // 嘗試建立 DIAL device,如果失敗代表那個設備不支援 DIAL picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); } else { // 請求送出參數到遠端設備的 App DialAppLaunchResult result = await app.RequestLaunchAsync(txtArgument.Text); if (result == DialAppLaunchResult.Launched) { activeDialDevice = args.SelectedDialDevice; DeviceInformation selectedDeviceInformation = await DeviceInformation.CreateFromIdAsync(args.SelectedDialDevice.Id); activeDeviceInformation = selectedDeviceInformation; picker.SetDisplayStatus(activeDialDevice, DialDeviceDisplayStatus.Connected); picker.Hide(); tblMsg.Text += "device connected"; } else { picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); tblMsg.Text += "device error"; } } } catch (Exception ex) { tblMsg.Text += ex.Message; } }); }
private async Task StopDialConnection(DevicePicker picker, DeviceInformation device) { try { // 取得被選擇的 dial device DialDevice selectedDialDevice = await DialDevice.FromIdAsync(device.Id); // 更新 picker status picker.SetDisplayStatus(device, "connecting", DevicePickerDisplayStatusOptions.ShowProgress); // 取得 dial app DialApp app = selectedDialDevice.GetDialApp("castingsample"); // 請求斷綫 DialAppStopResult result = await app.StopAsync(); if (result == DialAppStopResult.Stopped) { picker.SetDisplayStatus(device, "Disconnected", DevicePickerDisplayStatusOptions.None); activeDevice = null; picker.Hide(); } else { if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure) { // 如果失敗的話要記得多 retry 的機制 picker.SetDisplayStatus(device, "Error", DevicePickerDisplayStatusOptions.ShowDisconnectButton); } else { // 如果設備沒有支援 Stop 機制,則直接清楚連綫就好 activeDevice = null; } } } catch (Exception ex) { } }
private async Task SendDialParameter(DevicePicker sender, DeviceSelectedEventArgs args) { try { // 設定遠端設備現在要準備連綫 sender.SetDisplayStatus(args.SelectedDevice, "connecting", DevicePickerDisplayStatusOptions.ShowProgress); // 取得遠端設備中支援指定 app name 的 App DialDevice dialDevice = await DialDevice.FromIdAsync(args.SelectedDevice.Id); DialApp app = dialDevice.GetDialApp("castingsample"); if (app == null) { // 嘗試建立 DIAL device,如果失敗代表那個設備不支援 DIAL sender.SetDisplayStatus(args.SelectedDevice, "The app is not exist in the device.", DevicePickerDisplayStatusOptions.ShowRetryButton); } else { // 請求送出參數到遠端設備的 App DialAppLaunchResult result = await app.RequestLaunchAsync("Test"); if (result == DialAppLaunchResult.Launched) { activeDevice = args.SelectedDevice; sender.SetDisplayStatus(args.SelectedDevice, "connected", DevicePickerDisplayStatusOptions.ShowDisconnectButton); sender.Hide(); } else { sender.SetDisplayStatus(args.SelectedDevice, "Device Error", DevicePickerDisplayStatusOptions.ShowRetryButton); } } } catch (Exception ex) { sender.SetDisplayStatus(args.SelectedDevice, ex.Message, DevicePickerDisplayStatusOptions.None); } }
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 void watcherControlButton_Click(object sender, RoutedEventArgs e) { //Pause the video in the local player this.player.Pause(); StopCurrentWatcher(); this.castingDevicesList.Items.Clear(); CustomDevicePickerFilter filter = new CustomDevicePickerFilter(); //Add the CAST API Filter, so that the application only shows Miracast, Bluetooth, DLNA devices that can render the video //filter.SupportedDeviceSelectors.Add(await CastingDevice.GetDeviceSelectorFromCastingSourceAsync(this.player.GetAsCastingSource())); //Add the DIAL Filter, so that the application only shows DIAL devices that have the application installed or advertise that they can install them. filter.SupportedDeviceSelectors.Add(DialDevice.GetDeviceSelector(this.dial_appname_textbox.Text)); filter.SupportedDeviceSelectors.Add(ProjectionManager.GetDeviceSelector()); filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{D0875FB4-2196-4c7a-A63D-E416ADDD60A1}\"" + " AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True"); //Create our watcher and have it find casting devices capable of video casting watcher = DeviceInformation.CreateWatcher(filter.ToString()); //Register for watcher events watcher.Added += Watcher_Added; watcher.Removed += Watcher_Removed; watcher.Stopped += Watcher_Stopped; watcher.Updated += Watcher_Updated; watcher.EnumerationCompleted += Watcher_EnumerationCompleted; //start the watcher watcher.Start(); //update the UI to reflect the watcher's state rootPage.NotifyUser("Watcher has been started", NotifyType.StatusMessage); progressText.Text = "Searching"; progressRing.IsActive = true; }
private async void Picker_DisconnectButtonClicked(DialDevicePicker sender, DialDisconnectButtonClickedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { try { rootPage.NotifyUser(string.Format("Picker DisconnectButtonClicked event fired for device '{0}'", activeDeviceInformation.Name), NotifyType.StatusMessage); // Get the DialDevice instance for the selected device DialDevice selectedDialDevice = await DialDevice.FromIdAsync(args.Device.Id); // Update the picker status picker.SetDisplayStatus(selectedDialDevice, DialDeviceDisplayStatus.Connecting); DialApp app = selectedDialDevice.GetDialApp(this.dial_appname_textbox.Text); //Get the current application state //DialAppStateDetails stateDetails = await app.GetAppStateAsync(); DialAppStopResult result = await app.StopAsync(); if (result == DialAppStopResult.Stopped) { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser("Application stopped successfully.", NotifyType.StatusMessage); picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Disconnected); activeDialDevice = null; activeDeviceInformation = null; picker.Hide(); } else { if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure) { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser(string.Format("Error occured trying to stop application. Status: '{0}'", result.ToString()), NotifyType.StatusMessage); picker.SetDisplayStatus(args.Device, DialDeviceDisplayStatus.Error); } else //in case of DialAppStopResult.OperationNotSupported, there is not much more you can do. You could implement your own // mechanism to stop the application on that device. { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser(string.Format("Stop is not supported by device: '{0}'", activeDeviceInformation.Name), NotifyType.ErrorMessage); activeDialDevice = null; activeDeviceInformation = null; } } } catch (Exception ex) { UnhandledExceptionPage.ShowUnhandledException(ex); } }); }
private async Task <bool> TryLaunchDialAppAsync(DeviceInformation device) { bool dialAppLaunchSucceeded = false; if (device.Id.IndexOf("dial", StringComparison.OrdinalIgnoreCase) > -1) { //Update the launch arguments to include the Position this.dial_launch_args_textbox.Text = string.Format("v={0}&t={1}&pairingCode=E4A8136D-BCD3-45F4-8E49-AE01E9A46B5F", video.Id, player.Position.Ticks); //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("Checking to see if device {0} supports DIAL", device.Name), NotifyType.StatusMessage); //BUG: Takes too long. Workaround, just try to create the DialDevice //if (await DialDevice.DeviceInfoSupportsDialAsync(device)) //{ DialDevice dialDevice = null; //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("Attempting to resolve DIAL device for '{0}'", device.Name), NotifyType.StatusMessage); try { dialDevice = await DialDevice.FromIdAsync(device.Id); } catch { } if (dialDevice == 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 DIAL", device.Name), NotifyType.StatusMessage); } else { //Get the DialApp object for the specific application on the selected device DialApp app = dialDevice.GetDialApp(this.dial_appname_textbox.Text); if (app == null) { //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", device.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage); } else { //Get the current application state DialAppStateDetails stateDetails = await app.GetAppStateAsync(); if (stateDetails.State == DialAppState.NetworkFailure) { // In case getting the application state failed because of a network failure rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage); } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); //Launch the application on the 1st screen device DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text); //Verify to see whether the application was launched if (result == DialAppLaunchResult.Launched) { //Remember the device to which casting succeeded activeDevice = device; //DIAL is sessionsless but the DIAL app allows us to get the state and "disconnect". //Disconnect in the case of DIAL is equivalenet to stopping the app. activeCastConnectionHandler = app; rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage); //This is where you will need to add you application specific communication between your 1st and 2nd screen applications //... dialAppLaunchSucceeded = true; } } } } //} //else //{ // rootPage.NotifyUser(string.Format("'{0}' does not support DIAL", device.Name), NotifyType.StatusMessage); //} } else { rootPage.NotifyUser(string.Format("'{0}' does not support DIAL", device.Name), NotifyType.StatusMessage); } return(dialAppLaunchSucceeded); }
private async void Picker_DeviceSelected(DialDevicePicker sender, DialDeviceSelectedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { try { rootPage.NotifyUser(string.Format("Picker DeviceSelected event fired"), NotifyType.StatusMessage); // Set the status to connecting picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Connecting); rootPage.NotifyUser(string.Format("Resolving DialDevice'"), NotifyType.StatusMessage); //Get the DialApp object for the specific application on the selected device DialApp app = args.SelectedDialDevice.GetDialApp(this.dial_appname_textbox.Text); if (app == null) { //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. //rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", selectedDeviceInformation.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage); picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); //Launch the application on the 1st screen device DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text); //Verify to see whether the application was launched if (result == DialAppLaunchResult.Launched) { rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage); activeDialDevice = args.SelectedDialDevice; DeviceInformation selectedDeviceInformation = await DeviceInformation.CreateFromIdAsync(args.SelectedDialDevice.Id); activeDeviceInformation = selectedDeviceInformation; //This is where you will need to add you application specific communication between your 1st and 2nd screen applications //... //... //... picker.SetDisplayStatus(activeDialDevice, DialDeviceDisplayStatus.Connected); picker.Hide(); } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); } } } catch (Exception ex) { UnhandledExceptionPage.ShowUnhandledException(ex); } }); }
private async void Picker_DialDeviceSelected(DialDevicePicker sender, DialDeviceSelectedEventArgs args) { // casting 必須在 UI Thread 下執行 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { try { // 設定遠端設備現在要準備連綫 picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Connecting); // 取得遠端設備中支援指定 app name 的 App DialApp app = args.SelectedDialDevice.GetDialApp(txtAppName.Text); if (app == null) { // 嘗試建立 DIAL device,如果失敗代表那個設備不支援 DIAL picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); } else { // 請求送出參數到遠端設備的 App DialAppLaunchResult result = await app.RequestLaunchAsync(txtArgument.Text); if (result == DialAppLaunchResult.Launched) { activeDialDevice = args.SelectedDialDevice; DeviceInformation selectedDeviceInformation = await DeviceInformation.CreateFromIdAsync(args.SelectedDialDevice.Id); activeDeviceInformation = selectedDeviceInformation; picker.SetDisplayStatus(activeDialDevice, DialDeviceDisplayStatus.Connected); picker.Hide(); tblMsg.Text += "device connected"; } else { picker.SetDisplayStatus(args.SelectedDialDevice, DialDeviceDisplayStatus.Error); tblMsg.Text += "device error"; } } } catch (Exception ex) { tblMsg.Text += ex.Message; } }); }
private async void Picker_DisconnectButtonClicked(DialDevicePicker sender, DialDisconnectButtonClickedEventArgs args) { //Casting must occur from the UI thread. This dispatches the casting calls to the UI thread. 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. DialDevice selectedDevice = await DialDevice.FromIdAsync(args.Device.Id); // Get the DeviceInformation instance for the the selected device DeviceInformation selectedDeviceInfo = await DeviceInformation.CreateFromIdAsync(selectedDevice.Id); // Update the picker status try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Disconnecting); } catch { } DialApp app = args.Device.GetDialApp(this.dial_appname_textbox.Text); //Get the current application state DialAppStateDetails stateDetails = await app.GetAppStateAsync(); switch (stateDetails.State) { case DialAppState.NetworkFailure: { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage); // Update the picker status try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { } break; } case DialAppState.Stopped: { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser("Application was already stopped.", NotifyType.StatusMessage); try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Disconnected); } catch { } break; } default: { DialAppStopResult result = await app.StopAsync(); if (result == DialAppStopResult.Stopped) { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser("Application stopped successfully.", NotifyType.StatusMessage); try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Disconnected); } catch { } } else { if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure) { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser(string.Format("Error occured trying to stop application. Status: '{0}'", result.ToString()), NotifyType.StatusMessage); try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { } } else //in case of DialAppStopResult.OperationNotSupported, there is not much more you can do. You could implement your own // mechanism to stop the application on that device. { // In case getting the application state failed because of a network failure, you could add retry logic rootPage.NotifyUser(string.Format("Stop is not supported by device: '{0}'", selectedDeviceInfo.Name), NotifyType.ErrorMessage); } } break; } } }); }
private async Task <bool> TryLaunchDialAppAsync(DeviceInformation device) { bool dialAppLaunchSucceeded = false; //Update the launch arguments to include the Position this.dial_launch_args_textbox.Text = string.Format("v={0}&t={1}&pairingCode=E4A8136D-BCD3-45F4-8E49-AE01E9A46B5F", video.Id, player.Position.Ticks); //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("Checking to see if device {0} supports DIAL", device.Name), NotifyType.StatusMessage); DialDevice dialDevice = null; //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("Attempting to resolve DIAL device for '{0}'", device.Name), NotifyType.StatusMessage); string[] newIds = (string[])device.Properties["{0BBA1EDE-7566-4F47-90EC-25FC567CED2A} 2"]; if (newIds.Length > 0) { string deviceId = newIds[0]; try { dialDevice = await DialDevice.FromIdAsync(deviceId); } catch { } } if (dialDevice == 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 DIAL", device.Name), NotifyType.StatusMessage); } else { //Get the DialApp object for the specific application on the selected device DialApp app = dialDevice.GetDialApp(this.dial_appname_textbox.Text); if (app == null) { //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL. rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", device.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage); } else { rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage); //Launch the application on the 1st screen device DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text); //Verify to see whether the application was launched if (result == DialAppLaunchResult.Launched) { //Remember the device to which casting succeeded activeDevice = device; //DIAL is sessionsless but the DIAL app allows us to get the state and "disconnect". //Disconnect in the case of DIAL is equivalenet to stopping the app. activeCastConnectionHandler = app; rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage); //This is where you will need to add you application specific communication between your 1st and 2nd screen applications //... dialAppLaunchSucceeded = true; } } } return(dialAppLaunchSucceeded); }