private void InitEvent() { EventManager.RegisterClassHandler(typeof(VideoPlayerView), VideoPlayerView.MouseClickEvent, new RoutedEventHandler(MediaPlayerViewOnMouseClick)); EventManager.RegisterClassHandler(typeof(VideoPlayerView), VideoPlayerView.FullScreenClickEvent, new RoutedEventHandler(MediaPlayerViewOnFullScreenClick)); EventManager.RegisterClassHandler(typeof(VideoPlayerView), VideoPlayerView.RestoreScreenClickEvent, new RoutedEventHandler(MediaPlayerViewOnRestoreScreenClick)); Screens = new VideoPlayerView[9]; for (var i = 0; i < Screens.Length; i++) { var player = new VideoPlayerView { Margin = new Thickness(6) }; screenContain.Children.Add(player); Screens[i] = player; _playerDic.Add(player, null); } niceSplit.IsChecked = true; // 初始选中九分屏 playButton.Checked += PlayAllPlayer; playButton.Unchecked += StopAllPlayer; Observable.FromEventPattern <RoutedEventArgs>(this, nameof(Loaded)).Select(_ => "loaded") .Merge(Observable.FromEventPattern <RoutedEventArgs>(this, nameof(Unloaded)).Select(_ => "unloaded")) .Throttle(TimeSpan.FromMilliseconds(400)) .DistinctUntilChanged() //.Skip(1) .Subscribe(value => { switch (value) { case "loaded": Screens.ForEach(x => x.Replay()); break; case "unloaded": Screens.ForEach(x => x.Stop()); //GC.Collect(); break; } }); foreach (var player in _playerDic) { //player.Key.Play(@"C:\pci\HJMos_NCC_Client\HJMos_NCC_Client_V1.0\src\Hjmos.Ncc.WSV1\bin\Debug\Videos\01.mp4"); player.Key.Play(@"https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8"); } }
public VideoPanelView() { InitializeComponent(); grid.Rows = 2; for (var r = 0; r < 2; r++) { for (var c = 0; c < 2; c++) { var player = new VideoPlayerView { Margin = new Thickness(6) }; //Grid.SetRow(player, r); //Grid.SetColumn(player, c); grid.Children.Add(player); Players.Add(player); } } EventManager.RegisterClassHandler(typeof(VideoPlayerView), VideoPlayerView.MouseClickEvent, new RoutedEventHandler(( (sender, args) => { foreach (var screen in Players.Where(x => !Equals(x, args.OriginalSource))) { screen.HideBorder(); } }))); EventManager.RegisterClassHandler(typeof(VideoPlayerView), VideoPlayerView.FullScreenClickEvent, new RoutedEventHandler(( (sender, args) => { grid.Rows = 1; foreach (var screen in Players.Where(x => !Equals(x, args.OriginalSource))) { screen.Visibility = Visibility.Collapsed; } }))); EventManager.RegisterClassHandler(typeof(VideoPlayerView), VideoPlayerView.RestoreScreenClickEvent, new RoutedEventHandler(( (sender, args) => { grid.Rows = 2; foreach (var screen in Players) { screen.Visibility = Visibility.Visible; } }))); var loadOrUnload = Observable.FromEventPattern <RoutedEventArgs>(this, nameof(UserControl.Loaded)) .Select(_ => "loaded") .Merge(Observable.FromEventPattern <RoutedEventArgs>(this, nameof(UserControl.Unloaded)) .Select(_ => "unloaded")) .Throttle(TimeSpan.FromMilliseconds(500)) .DistinctUntilChanged(); loadOrUnload.ObserveOnDispatcher() .Skip(1) .Subscribe(value => { Players.ForEach(x => { switch (value) { case "loaded": x.Replay(); break; case "unloaded": x.Stop(); break; } }); }, ex => LogHelper.Error($"视频面板发生异常,{ex}")); _idsSubject.Switch() .SkipUntil(loadOrUnload.Where(x => x == "loaded")) .TakeUntil(loadOrUnload.Where(x => x == "unloaded")) .Repeat() .ObserveOnDispatcher() .Subscribe(async x => { if (Responses?.Count > 0) { var stopVideoRequest = new StopVideoRequest { PlayList = Responses.Select(y => new PlayListItem { Url = y.Url, CameraId = y.CameraId, SessionId = y.SessionId }).ToList() }; await ContainerLocator.Container.Resolve <IVideoService>()?.StopVideo(stopVideoRequest); Responses = null; } if (x.Count == 0) { Players.ForEach(player => player.Stop()); return; } var result = await ContainerLocator.Container.Resolve <IVideoService>()?.PlayVideo(x.ToArray()); Responses = result; for (var i = 0; i < Players.Count; i++) { if (result.Count > i) { Players[i].Play(result[i].Url); } else { Players[i].Stop(); } } }, ex => LogHelper.Error($"视频面板发生异常,{ex}")); _subject.Throttle(TimeSpan.FromMilliseconds(500)) .DistinctUntilChanged(x => { var builder = new StringBuilder(); var(interval, ids) = x; builder.Append(interval); foreach (var id in ids) { builder.Append(id); } //ids.ForEach(y => builder.Append(y)); return(builder.ToString()); }) .Select(x => { var(interval, ids) = x; var o = Observable.Interval(TimeSpan.FromSeconds(interval)) .Select(tick => tick + 1) .StartWith(0) .Select(tick => { if (ids.Length == 0) { return(new List <long>()); } var start = (4 * tick) % ids.Length; var end = start + 4 > ids.Length ? ids.Length : start + 4; var list = new List <long>(4); for (var i = start; i < end; i++) { list.Add(ids[i]); } return(list); }); return(o); }) .Subscribe(x => { _idsSubject.OnNext(x); }, ex => LogHelper.Error($"视频面板发生异常,{ex}")); }