0

我目前正在开发一个独立的应用程序,它可以处理来自移动设备的通信,还可以利用 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);
        }
    }

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

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

4

1 回答 1

0

我认为您的问题可以改写为“如何从 SignalR Hub 在我的 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(...);
于 2020-01-19T09:15:49.787 回答