DFVideoControl.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. namespace InABox.DynamicGrid
  11. {
  12. public class DFVideoControl : DynamicFormFieldControl<DFLayoutVideoField, DFLayoutVideoFieldProperties, byte[]>
  13. {
  14. Button PlayButton;
  15. Button PauseButton;
  16. MediaElement Player;
  17. bool firstPlay = true;
  18. byte[]? Data;
  19. protected override FrameworkElement Create()
  20. {
  21. var grid = new Grid();
  22. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  23. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  24. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  25. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  26. PlayButton = new Button
  27. {
  28. Width = 25,
  29. Height = 25,
  30. Margin = new Thickness(5)
  31. };
  32. PlayButton.Content = new Image
  33. {
  34. Source = Wpf.Resources.play_button.AsBitmapImage(),
  35. };
  36. PlayButton.Click += PlayButton_Click;
  37. PlayButton.SetGridPosition(0, 0);
  38. PauseButton = new Button
  39. {
  40. Width = 25,
  41. Height = 25,
  42. Margin = new Thickness(5),
  43. Visibility = Visibility.Hidden
  44. };
  45. PauseButton.Content = new Image
  46. {
  47. Source = Wpf.Resources.pause_button.AsBitmapImage()
  48. };
  49. PauseButton.Click += PauseButton_Click;
  50. PauseButton.SetGridPosition(0, 0);
  51. Player = new MediaElement();
  52. Player.Stretch = System.Windows.Media.Stretch.Fill;
  53. Player.Visibility = Visibility.Visible;
  54. Player.SetGridPosition(1, 0, 1, 2);
  55. grid.Children.Add(PlayButton);
  56. grid.Children.Add(PauseButton);
  57. grid.Children.Add(Player);
  58. return grid;
  59. }
  60. private void PlayButton_Click(object sender, RoutedEventArgs e)
  61. {
  62. var path = System.IO.Path.GetTempFileName();
  63. path = path.Substring(0, path.Length - 4) + ".mp4";
  64. System.IO.File.WriteAllBytes(path, GetValue());
  65. Player.Source = new Uri(path);
  66. firstPlay = false;
  67. }
  68. private void PauseButton_Click(object sender, RoutedEventArgs e)
  69. {
  70. Player.Pause();
  71. PauseButton.Visibility = Visibility.Hidden;
  72. PlayButton.Visibility = Visibility.Visible;
  73. }
  74. public override byte[] GetValue()
  75. {
  76. return Data ?? Array.Empty<byte>();
  77. }
  78. public override void SetValue(byte[]? value)
  79. {
  80. Data = value;
  81. }
  82. protected override bool IsEmpty() => Data is null || Data.Length == 0;
  83. }
  84. }