使用Windows.Media.Miracast命名空间的正确方法是什么?

时间:2019-05-09 12:30:04

标签: c# windows windows-10 miracast

我正在研究在应用程序中通过Miracast进行屏幕投射,但是不确定如何使用Windows.Media.Miracast命名空间。由于Windows 10 1903更新的时间很短,因此名称空间是其中的一部分,因此互联网上存在的信息有限。

到目前为止,我唯一发现的是this documentation

我的问题是,有人知道使用此命名空间的正确方法是什么吗?在线上找到的任何示例或资源都会有很大帮助。

干杯。

1 个答案:

答案 0 :(得分:0)

这三个示例项目演示了可以在UWP应用程序中使用的各种MiraCast源api。不确定外部UWP。

我个人在Windows IoT核心版上使用如下代码来投射整个屏幕

扫描设备:

miraDeviceWatcher = DeviceInformation.CreateWatcher(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); 
miraHandlerAdded = new TypedEventHandler<DeviceWatcher, DeviceInformation>(async (watcher, deviceInfo) =>
{
   await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
   {
      //Add each discovered device to our listbox
      CastingDevice addedDevice = await CastingDevice.FromIdAsync(deviceInfo.Id);
      var disp = new CastingDisplay(addedDevice); //my viewmodel
      MiraDevices.Add(disp); //ObservableCollection
   });
});
miraDeviceWatcher.Added += miraHandlerAdded;

连接到所选设备:

public async Task StartCasting(CastingDisplay castee)
{
   //When a device is selected, first thing we do is stop the watcher so it's search doesn't conflict with streaming
   if (miraDeviceWatcher.Status != DeviceWatcherStatus.Stopped)
   {
      miraDeviceWatcher.Stop();
   }

   //Create a new casting connection to the device that's been selected
   connection = castee.Device.CreateCastingConnection();
   //Register for events
   connection.ErrorOccurred += Connection_ErrorOccurred;
   connection.StateChanged += Connection_StateChangedAsync;

   var image = new Windows.UI.Xaml.Controls.Image();
   await connection.RequestStartCastingAsync(image.GetAsCastingSource());
}

此图像仅用作投射源。建立连接后,将播放我的整个屏幕。该行为没有记录。希望它在以后的更新中不会得到“修复”。