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

SignalR WPF server - How to trigger a player?

我目前正在开发一个独立的应用程序,它可以处理来自移动设备的通信,还可以利用 VLC 播放器输出流。 我这样做的方式是输出流对象在主 window 中,但我通过 SignalR 接收请求。 请求相当简单——它只是一个字符串。问题是我不知道如何将字符串从 SignalR 集线器传递回 MediaPlayer 对象。如何将字符串传递给外部对象或在集线器内制作媒体播放器 "persist"?现在我的代码看起来像这样:

枢纽:


    [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);
        }
    }

VLC控件:

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));
        }
    }

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

我认为您的问题可以改写为 "How can I call a method on my UI from a SignalR Hub"

为此,您有多种选择:

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

在您的视图构造函数中,在 InitializeComponents 之后:

ViewAccessor.View = this;

在您的中心:

ViewAccessor.View.Play(...);