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 { } }); }
private async void Picker_CastingDeviceSelected(CastingDevicePicker sender, CastingDeviceSelectedEventArgs args) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { connection = args.SelectedCastingDevice.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 CastingConnectionErrorStatus status = await connection.RequestStartCastingAsync(source); if (status == CastingConnectionErrorStatus.Succeeded) { player.Play(); } } catch { } }); }
private async void Picker_CastingDeviceSelected(CastingDevicePicker sender, CastingDeviceSelectedEventArgs args) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { try { //rootPage.NotifyUser(string.Format("Picker DeviceSelected event fired for device '{0}'", args.SelectedCastingDevice.FriendlyName), NotifyType.StatusMessage); DateTime t1 = DateTime.Now; DeviceInformation mydevice = await DeviceInformation.CreateFromIdAsync(args.SelectedCastingDevice.Id); DateTime t2 = DateTime.Now; TimeSpan ts = new TimeSpan(t2.Ticks - t1.Ticks); //System.Diagnostics.Debug.WriteLine(string.Format("DeviceInformation.CreateFromIdAsync took '{0} seconds'", ts.TotalSeconds)); //Create a casting conneciton from our selected casting device //rootPage.NotifyUser(string.Format("Creating connection for '{0}'", args.SelectedCastingDevice.FriendlyName), NotifyType.StatusMessage); connection = args.SelectedCastingDevice.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}'", args.SelectedCastingDevice.FriendlyName), NotifyType.StatusMessage); CastingConnectionErrorStatus status = await connection.RequestStartCastingAsync(source); if (status == CastingConnectionErrorStatus.Succeeded) { player.Play(); //rootPage.NotifyUser(string.Format("Starting casting to '{0}'", args.SelectedCastingDevice.FriendlyName), NotifyType.StatusMessage); } } catch { //rootPage.NotifyUser(string.Format("Failed to get casting source for video '{0}'", video.Title), NotifyType.ErrorMessage); } } catch (Exception ex) { #if DEBUG System.Diagnostics.Debug.WriteLine(ex.Message); #endif } }); }
private async Task <bool> TryDisconnectCastingSessionAsync(CastingConnection connection) { bool disconnected = false; //Disconnect the casting session CastingConnectionErrorStatus status = await connection.DisconnectAsync(); if (status == CastingConnectionErrorStatus.Succeeded) { rootPage.NotifyUser("Connection disconnected successfully.", NotifyType.StatusMessage); disconnected = true; } else { rootPage.NotifyUser(string.Format("Failed to disconnect connection with reason {0}.", status.ToString()), NotifyType.ErrorMessage); } return(disconnected); }
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 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); }