SignalR WPF 服务器 - 如何触发播放器?



我目前正在开发一个独立的应用程序,该应用程序可以处理来自移动设备的通信,还可以利用VLC播放器输出流。 我这样做的方法是输出流对象在主窗口中,但我通过 SignalR 接收请求。 请求相当简单 - 它只是一个字符串。问题是我不知道如何将字符串从 SignalR 中心传递回媒体播放器对象。如何将字符串传递给外部的对象或使媒体播放器"持久化"在集线器内?现在,我关于它的代码如下所示:

中心:


[HubName("CommHub")]
public class CommHub:Hub
{
VLCControl outputplayer = new VLCControl();
public void RequestConnectionsList()            
{
var databasePath = "--dbpath here--";
var db = new SQLiteConnection(databasePath);
List<string> output = db.Table<VideoSources>().Select(p => p.Name).ToList();    //Gets names from the db
Clients.Client(Context.ConnectionId).CamsInfo(output); //send available connection names to client
}
public void RequestOutStream(string requestedName) //this function is called but i have no idea how to make it work
{
outputplayer.playSelected(requestedName);
}
}

VLCControl:

class VLCControl
{
public Media rtsp;
private const string VIDEO_URL = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
private MediaPlayer player;
public static string GetConfigurationString()       //using this method in mainwindow player as well
{
string address = Properties.Settings.Default.LocalAddress;
string port = Properties.Settings.Default.LocalPort;
string result=
":sout=#duplicate" +
"{dst=display{noaudio}," +
"dst=rtp{mux=ts,dst=" + address +
",port=" + port + ",sdp=rtsp://" + address + ":" + port + "/go.sdp}";
return result;
}
public void playSelected(string inputAddress)
{
var databasePath = "D:\Projects\Sowa\Sowa\Serwer\VideoSources.db";
var db = new SQLiteConnection(databasePath);
string input = db.Table<VideoSources>().FirstOrDefault(p => p.Name == "test").Address;
db.Close();
var rtsp = new Media(MainWindow._libvlc, input, FromType.FromLocation);
rtsp.AddOption(VLCControl.GetConfigurationString());
player.Stop();
player.Play(new Media(MainWindow._libvlc, VIDEO_URL, FromType.FromLocation));
}
}

播放器肯定在工作 - 当我在主窗口中创建媒体播放器时,它确实按预期输出。

我认为你的问题可以改写为"如何从 SignalR 中心在我的 UI 上调用方法">

为此,您有几种选择:

  • 如果使用 ASP.net 核心的 SignalR,则可以使用依赖项注入将窗口或访问器注入窗口
  • 您可以使用(MyWindow)Application.Current.MainWindow获取主窗口。你用它做什么取决于你
  • 您可以创建一个静态类,该类将直接保存对组件的引用。此示例假定应用程序中一次只有一个视图。
public static class ViewAccessor {
public static MyView View { get; set; }
}

在您的视图构造函数中,在初始化组件之后:

ViewAccessor.View = this;

在您的中心:

ViewAccessor.View.Play(...);

最新更新