我们设法使用 UWP 创建了一个可用的 Miracast 接收器,我们希望在 .NET Core 应用程序中使用此功能。因此,我们按照本指南在 .NET Core 项目中使用 UWP api:
将 UWP api 与 WPF 和 .NET Core 结合使用
项目运行,我们获得了从智能手机到应用程序的连接,但是我们没有收到来自 MediaPlayer 对象的任何视频帧(与正常工作的原始 UWP 项目不同)
我们观察到在 MediaSource 对象中我们获得了一个 mcrecv url (Example -> mcrecv://192.168.137.247:7236/h-0000000c/192.168.137.1)
但随后使用它的 MediaPlayer 不会触发任何 VideoFrameAvailable 事件。
我们如何解决这个问题?以下是我们使用的基本实现:
using System;
using System.Diagnostics;
using System.Windows;
using Windows.Graphics.Imaging;
using Windows.Media.Miracast;
using Windows.Media.Playback;
namespace Miracast_GUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MiracastReceiver receiver;
public MiracastReceiverSession session;
public MediaPlayer mp;
public SoftwareBitmap frameServerDest;
public MiracastReceiverConnection connection;
public MainWindow()
{
InitializeComponent();
// Starts service
StartMiracastService();
}
public void StartMiracastService()
{
receiver = new MiracastReceiver();
receiver.StatusChanged += Receiver_StatusChanged;
MiracastReceiverSettings settings = receiver.GetDefaultSettings();
settings.FriendlyName = "Miracast-Service-Test";
settings.AuthorizationMethod = MiracastReceiverAuthorizationMethod.None;
settings.ModelName = receiver.GetDefaultSettings().ModelName;
settings.ModelNumber = receiver.GetDefaultSettings().ModelNumber;
settings.RequireAuthorizationFromKnownTransmitters = receiver.GetDefaultSettings().RequireAuthorizationFromKnownTransmitters;
receiver.DisconnectAllAndApplySettings(settings);
session = receiver.CreateSession(/*CoreApplication.MainView*/ null);
session.AllowConnectionTakeover = true;
session.ConnectionCreated += Session_ConnectionCreated;
session.MediaSourceCreated += Session_MediaSourceCreated;
session.Disconnected += Session_Disconnected;
MiracastReceiverSessionStartResult result = session.Start();
Debug.WriteLine("Status: " + result.Status);
}
private void Session_Disconnected(MiracastReceiverSession sender, MiracastReceiverDisconnectedEventArgs args)
{
session.Dispose();
}
private void Receiver_StatusChanged(MiracastReceiver sender, object args)
{
Debug.WriteLine(receiver.GetStatus().ListeningStatus);
}
private void Session_ConnectionCreated(MiracastReceiverSession sender, MiracastReceiverConnectionCreatedEventArgs args)
{
connection = args.Connection;
connection.InputDevices.Keyboard.TransmitInput = true;
connection.InputDevices.GameController.Mode =
MiracastReceiverGameControllerDeviceUsageMode.AsMouseAndKeyboard;
Debug.WriteLine("CONNECTION CREATED");
}
private void Session_MediaSourceCreated(MiracastReceiverSession sender, MiracastReceiverMediaSourceCreatedEventArgs args)
{
mp = new MediaPlayer
{
IsVideoFrameServerEnabled = true,
AutoPlay = true,
Source = args.MediaSource,
RealTimePlayback = true
};
mp.VideoFrameAvailable += Mp_VideoFrameAvailable;
Debug.WriteLine(mp.PlaybackSession.PlaybackState);
mp.Play();
Debug.WriteLine("MEDIA SOURCE CREATED");
}
private void Mp_VideoFrameAvailable(MediaPlayer sender, object args)
{
Console.WriteLine("Received frame...");
}
}
}