DFVideoControl.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using Microsoft.Xaml.Behaviors.Core;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class DFVideoControl : DynamicFormFieldControl<DFLayoutVideoField, DFLayoutVideoFieldProperties, byte[], DFLayoutEmbeddedMediaValue>
  14. {
  15. private DFLayoutEmbeddedMediaValue _value = null!;
  16. private Button? _playButton;
  17. private Button? _pauseButton;
  18. private Button? _downloadButton;
  19. private MediaElement _player = null!;
  20. private Border? _border;
  21. private Image? _thumbnail;
  22. private Grid? _contentGrid;
  23. private Grid? _grid;
  24. /*Point center;
  25. int rotation = 90;
  26. bool firstPlay = true;*/
  27. private string _tempfilename = null!;
  28. byte[]? Data;
  29. protected override FrameworkElement Create()
  30. {
  31. _tempfilename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(),".mp4");
  32. _value = new DFLayoutEmbeddedMediaValue();
  33. _grid = new Grid();
  34. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  35. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  36. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  37. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  38. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  39. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  40. _border = new Border()
  41. {
  42. BorderBrush = new System.Windows.Media.SolidColorBrush(Colors.Gray),
  43. BorderThickness = new Thickness(0.75),
  44. Background = new System.Windows.Media.SolidColorBrush(Colors.White),
  45. Padding = new Thickness(4),
  46. Margin = new Thickness(0)
  47. };
  48. _border.SetGridPosition(0,0,5,1);
  49. _grid.Children.Add(_border);
  50. _contentGrid = new Grid();
  51. _contentGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  52. _contentGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  53. _border.Child = _contentGrid;
  54. _thumbnail = new Image
  55. {
  56. StretchDirection = StretchDirection.DownOnly,
  57. Source = EmbeddedImageUtilities.CreateEmptyImage()
  58. };
  59. _thumbnail.Visibility = Visibility.Visible;
  60. _thumbnail.SetGridPosition(0, 0, 4, 1);
  61. _contentGrid.Children.Add(_thumbnail);
  62. _player = new MediaElement
  63. {
  64. Stretch = System.Windows.Media.Stretch.Fill,
  65. Visibility = Visibility.Visible,
  66. VerticalAlignment = VerticalAlignment.Center,
  67. HorizontalAlignment = HorizontalAlignment.Center,
  68. LoadedBehavior = MediaState.Manual,
  69. Source = new Uri(_tempfilename)
  70. };
  71. _player.Visibility = Visibility.Collapsed;
  72. _player.MediaOpened += (sender, args) =>
  73. {
  74. _player.Visibility = Visibility.Visible;
  75. _thumbnail.Visibility = Visibility.Collapsed;
  76. };
  77. _player.MediaEnded += (sender, args) =>
  78. {
  79. _player.Visibility = Visibility.Collapsed;
  80. _thumbnail.Visibility = Visibility.Visible;
  81. _player.Close();
  82. };
  83. _player.SetGridPosition(0, 0, 4, 1);
  84. _contentGrid.Children.Add(_player);
  85. _playButton = new Button
  86. {
  87. Margin = new Thickness(5,0,0,0),
  88. Content = new Image { Source = Wpf.Resources.play_button.AsBitmapImage(24,24) },
  89. Command = new ActionCommand(PlayButton_Click),
  90. Width = 30,
  91. Height = 30
  92. };
  93. _playButton.SetGridPosition(0, 1);
  94. _grid.Children.Add(_playButton);
  95. _pauseButton = new Button
  96. {
  97. Margin = new Thickness(5,5,0,0),
  98. Content = new Image { Source = Wpf.Resources.pause_button.AsBitmapImage(32,32) },
  99. Command = new ActionCommand(PauseButton_Click),
  100. Width = 30,
  101. Height = 30
  102. };
  103. _pauseButton.SetGridPosition(1, 1);
  104. _grid.Children.Add(_pauseButton);
  105. _downloadButton = new Button
  106. {
  107. Margin = new Thickness(5,5,0,0),
  108. Content = new Image { Source = Wpf.Resources.download.AsBitmapImage(32,32) },
  109. Command = new ActionCommand(DownloadButton_Click),
  110. Width = 30,
  111. Height = 30
  112. };
  113. _downloadButton.SetGridPosition(3, 1);
  114. _grid.Children.Add(_downloadButton);
  115. return _grid;
  116. }
  117. private void DownloadButton_Click()
  118. {
  119. var dlg = new SaveFileDialog();
  120. dlg.AddExtension = true;
  121. dlg.DefaultExt = ".mp4";
  122. dlg.ShowDialog();
  123. if (!string.IsNullOrWhiteSpace(dlg.FileName))
  124. {
  125. System.IO.File.WriteAllBytes(dlg.FileName, GetValue());
  126. MessageBox.Show("Success - file saved to: "
  127. + System.Environment.NewLine
  128. + dlg.FileName);
  129. var dir = System.IO.Path.GetDirectoryName(dlg.FileName);
  130. ProcessStartInfo startInfo = new ProcessStartInfo
  131. {
  132. Arguments = dir,
  133. FileName = "explorer.exe"
  134. };
  135. Process.Start(startInfo);
  136. }
  137. }
  138. private void PauseButton_Click()
  139. {
  140. _player.Pause();
  141. }
  142. private void PlayButton_Click()
  143. {
  144. if (System.IO.File.Exists(_tempfilename))
  145. {
  146. _player.Play();
  147. }
  148. else
  149. {
  150. DigitalFormDocumentFactory.LoadDocument(_value.ID, (data) =>
  151. {
  152. Dispatcher.BeginInvoke(() =>
  153. {
  154. System.IO.File.WriteAllBytes(_tempfilename, data);
  155. _player.Position = new TimeSpan(0);
  156. _player.Play();
  157. });
  158. });
  159. }
  160. }
  161. public override DFLayoutEmbeddedMediaValue GetSerializedValue()
  162. {
  163. if ((_value.Data?.Any() == true) && (_value.ID == Guid.Empty))
  164. _value.ID = DigitalFormDocumentFactory.SaveDocument(_value.Data);
  165. return _value;
  166. }
  167. public override void SetSerializedValue(DFLayoutEmbeddedMediaValue value)
  168. {
  169. _value = value;
  170. EmbeddedImageUtilities.LoadImageFromData(_thumbnail, _value.Thumbnail);
  171. }
  172. public override byte[] GetValue()
  173. {
  174. return Data ?? Array.Empty<byte>();
  175. }
  176. public override void SetValue(byte[]? value)
  177. {
  178. Data = value;
  179. }
  180. protected override bool IsEmpty() => Data is null || Data.Length == 0;
  181. }
  182. }