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

问题描述 投票:0回答:1

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

中心:


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

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

wpf signalr signalr-hub libvlcsharp
1个回答
0
投票

我认为您的问题可以改写为“如何从SignalR集线器在UI上调用方法”

为此,您有几种选择:

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

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

ViewAccessor.View = this;

在您的中心:

ViewAccessor.View.Play(...);
© www.soinside.com 2019 - 2024. All rights reserved.