NavigationBar.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using MediaBrowser.UI.Controller;
  2. using MediaBrowser.UI.Playback;
  3. using MediaBrowser.UI.Playback.InternalPlayer;
  4. using System;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Input;
  9. namespace MediaBrowser.UI.Controls
  10. {
  11. /// <summary>
  12. /// Interaction logic for NavigationBar.xaml
  13. /// </summary>
  14. public partial class NavigationBar : BaseUserControl
  15. {
  16. /// <summary>
  17. /// Gets or sets the current player.
  18. /// </summary>
  19. /// <value>The current player.</value>
  20. private BaseMediaPlayer CurrentPlayer { get; set; }
  21. /// <summary>
  22. /// Gets or sets the current position timer.
  23. /// </summary>
  24. /// <value>The current position timer.</value>
  25. private Timer CurrentPositionTimer { get; set; }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="NavigationBar" /> class.
  28. /// </summary>
  29. public NavigationBar()
  30. {
  31. InitializeComponent();
  32. Loaded += NavigationBar_Loaded;
  33. }
  34. /// <summary>
  35. /// Handles the Loaded event of the NavigationBar control.
  36. /// </summary>
  37. /// <param name="sender">The source of the event.</param>
  38. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  39. void NavigationBar_Loaded(object sender, RoutedEventArgs e)
  40. {
  41. BackButton.Click += BtnApplicationBackClick;
  42. MuteButton.Click += MuteButton_Click;
  43. VolumeDownButton.PreviewMouseDown += VolumeDownButton_Click;
  44. VolumeUpButton.PreviewMouseDown += VolumeUpButton_Click;
  45. StopButton.Click += StopButton_Click;
  46. PlayButton.Click += PlayButton_Click;
  47. PauseButton.Click += PauseButton_Click;
  48. NextChapterButton.Click += NextChapterButton_Click;
  49. PreviousChapterButton.Click += PreviousChapterButton_Click;
  50. UIKernel.Instance.PlaybackManager.PlaybackStarted += PlaybackManager_PlaybackStarted;
  51. UIKernel.Instance.PlaybackManager.PlaybackCompleted += PlaybackManager_PlaybackCompleted;
  52. CurrentPositionSlider.PreviewMouseUp += CurrentPositionSlider_PreviewMouseUp;
  53. }
  54. /// <summary>
  55. /// Handles the Click event of the PreviousChapterButton control.
  56. /// </summary>
  57. /// <param name="sender">The source of the event.</param>
  58. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  59. async void PreviousChapterButton_Click(object sender, RoutedEventArgs e)
  60. {
  61. await CurrentPlayer.GoToPreviousChapter();
  62. }
  63. /// <summary>
  64. /// Handles the Click event of the NextChapterButton control.
  65. /// </summary>
  66. /// <param name="sender">The source of the event.</param>
  67. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  68. async void NextChapterButton_Click(object sender, RoutedEventArgs e)
  69. {
  70. await CurrentPlayer.GoToNextChapter();
  71. }
  72. /// <summary>
  73. /// Handles the Click event of the PauseButton control.
  74. /// </summary>
  75. /// <param name="sender">The source of the event.</param>
  76. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  77. async void PauseButton_Click(object sender, RoutedEventArgs e)
  78. {
  79. await CurrentPlayer.Pause();
  80. }
  81. /// <summary>
  82. /// Handles the Click event of the PlayButton control.
  83. /// </summary>
  84. /// <param name="sender">The source of the event.</param>
  85. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  86. async void PlayButton_Click(object sender, RoutedEventArgs e)
  87. {
  88. await CurrentPlayer.UnPause();
  89. }
  90. /// <summary>
  91. /// Handles the Click event of the StopButton control.
  92. /// </summary>
  93. /// <param name="sender">The source of the event.</param>
  94. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  95. async void StopButton_Click(object sender, RoutedEventArgs e)
  96. {
  97. await CurrentPlayer.Stop();
  98. }
  99. /// <summary>
  100. /// Handles the PlaybackCompleted event of the PlaybackManager control.
  101. /// </summary>
  102. /// <param name="sender">The source of the event.</param>
  103. /// <param name="e">The <see cref="PlaybackEventArgs" /> instance containing the event data.</param>
  104. void PlaybackManager_PlaybackCompleted(object sender, PlaybackStopEventArgs e)
  105. {
  106. if (e.Player == CurrentPlayer)
  107. {
  108. if (CurrentPositionTimer != null)
  109. {
  110. CurrentPositionTimer.Dispose();
  111. }
  112. CurrentPlayer.PlayStateChanged -= CurrentPlayer_PlayStateChanged;
  113. CurrentPlayer = null;
  114. ResetButtonVisibilities(null);
  115. Dispatcher.InvokeAsync(() => TxtCurrentPosition.Text = string.Empty);
  116. }
  117. }
  118. /// <summary>
  119. /// Handles the Click event of the VolumeUpButton control.
  120. /// </summary>
  121. /// <param name="sender">The source of the event.</param>
  122. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  123. void VolumeUpButton_Click(object sender, RoutedEventArgs e)
  124. {
  125. CurrentPlayer.Volume += 3;
  126. }
  127. /// <summary>
  128. /// Handles the Click event of the VolumeDownButton control.
  129. /// </summary>
  130. /// <param name="sender">The source of the event.</param>
  131. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  132. void VolumeDownButton_Click(object sender, RoutedEventArgs e)
  133. {
  134. CurrentPlayer.Volume -= 3;
  135. }
  136. /// <summary>
  137. /// Handles the Click event of the MuteButton control.
  138. /// </summary>
  139. /// <param name="sender">The source of the event.</param>
  140. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  141. void MuteButton_Click(object sender, RoutedEventArgs e)
  142. {
  143. if (CurrentPlayer.CanMute)
  144. {
  145. CurrentPlayer.Mute = !CurrentPlayer.Mute;
  146. }
  147. }
  148. /// <summary>
  149. /// Handles the PlaybackStarted event of the PlaybackManager control.
  150. /// </summary>
  151. /// <param name="sender">The source of the event.</param>
  152. /// <param name="e">The <see cref="PlaybackEventArgs" /> instance containing the event data.</param>
  153. void PlaybackManager_PlaybackStarted(object sender, PlaybackEventArgs e)
  154. {
  155. if (e.Player is BaseInternalMediaPlayer)
  156. {
  157. CurrentPlayer = e.Player;
  158. CurrentPlayer.PlayStateChanged += CurrentPlayer_PlayStateChanged;
  159. ResetButtonVisibilities(e.Player);
  160. Dispatcher.InvokeAsync(() =>
  161. {
  162. var runtime = e.Player.CurrentMedia.RunTimeTicks ?? 0;
  163. CurrentPositionSlider.Maximum = runtime;
  164. TxtDuration.Text = GetTimeString(runtime);
  165. });
  166. CurrentPositionTimer = new Timer(CurrentPositionTimerCallback, null, 250, 250);
  167. }
  168. }
  169. /// <summary>
  170. /// Currents the position timer callback.
  171. /// </summary>
  172. /// <param name="state">The state.</param>
  173. private void CurrentPositionTimerCallback(object state)
  174. {
  175. var time = string.Empty;
  176. var ticks = CurrentPlayer.CurrentPositionTicks;
  177. if (ticks.HasValue)
  178. {
  179. time = GetTimeString(ticks.Value);
  180. }
  181. Dispatcher.InvokeAsync(() =>
  182. {
  183. TxtCurrentPosition.Text = time;
  184. if (!_isPositionSliderDragging)
  185. {
  186. CurrentPositionSlider.Value = ticks ?? 0;
  187. }
  188. });
  189. }
  190. /// <summary>
  191. /// Gets the time string.
  192. /// </summary>
  193. /// <param name="ticks">The ticks.</param>
  194. /// <returns>System.String.</returns>
  195. private string GetTimeString(long ticks)
  196. {
  197. var timespan = TimeSpan.FromTicks(ticks);
  198. return timespan.TotalHours >= 1 ? timespan.ToString("hh':'mm':'ss") : timespan.ToString("mm':'ss");
  199. }
  200. /// <summary>
  201. /// Handles the PlayStateChanged event of the CurrentPlayer control.
  202. /// </summary>
  203. /// <param name="sender">The source of the event.</param>
  204. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  205. void CurrentPlayer_PlayStateChanged(object sender, EventArgs e)
  206. {
  207. ResetButtonVisibilities(CurrentPlayer);
  208. }
  209. /// <summary>
  210. /// Resets the button visibilities.
  211. /// </summary>
  212. /// <param name="player">The player.</param>
  213. private void ResetButtonVisibilities(BaseMediaPlayer player)
  214. {
  215. Dispatcher.Invoke(() =>
  216. {
  217. PlayButton.Visibility = player != null && player.PlayState == PlayState.Paused ? Visibility.Visible : Visibility.Collapsed;
  218. PauseButton.Visibility = player != null && player.CanPause && player.PlayState == PlayState.Playing ? Visibility.Visible : Visibility.Collapsed;
  219. StopButton.Visibility = player != null ? Visibility.Visible : Visibility.Collapsed;
  220. MuteButton.Visibility = player != null && player.CanMute ? Visibility.Visible : Visibility.Collapsed;
  221. VolumeUpButton.Visibility = player != null && player.CanControlVolume ? Visibility.Visible : Visibility.Collapsed;
  222. VolumeDownButton.Visibility = player != null && player.CanControlVolume ? Visibility.Visible : Visibility.Collapsed;
  223. var isSeekabke = player != null && player.CanSeek && player.CurrentMedia != null;
  224. SeekGrid.Visibility = isSeekabke ? Visibility.Visible : Visibility.Collapsed;
  225. var canSeekChapters = isSeekabke && player.CurrentMedia.Chapters != null && player.CurrentMedia.Chapters.Count > 1;
  226. NextChapterButton.Visibility = canSeekChapters ? Visibility.Visible : Visibility.Collapsed;
  227. PreviousChapterButton.Visibility = canSeekChapters ? Visibility.Visible : Visibility.Collapsed;
  228. });
  229. }
  230. /// <summary>
  231. /// BTNs the application back click.
  232. /// </summary>
  233. /// <param name="sender">The sender.</param>
  234. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  235. void BtnApplicationBackClick(object sender, RoutedEventArgs e)
  236. {
  237. App.Instance.ApplicationWindow.NavigateBack();
  238. }
  239. /// <summary>
  240. /// The is position slider dragging
  241. /// </summary>
  242. private bool _isPositionSliderDragging;
  243. /// <summary>
  244. /// Handles the DragStarted event of the CurrentPositionSlider control.
  245. /// </summary>
  246. /// <param name="sender">The source of the event.</param>
  247. /// <param name="e">The <see cref="DragStartedEventArgs" /> instance containing the event data.</param>
  248. private void CurrentPositionSlider_DragStarted(object sender, DragStartedEventArgs e)
  249. {
  250. _isPositionSliderDragging = true;
  251. }
  252. /// <summary>
  253. /// Handles the DragCompleted event of the CurrentPositionSlider control.
  254. /// </summary>
  255. /// <param name="sender">The source of the event.</param>
  256. /// <param name="e">The <see cref="DragCompletedEventArgs" /> instance containing the event data.</param>
  257. private void CurrentPositionSlider_DragCompleted(object sender, DragCompletedEventArgs e)
  258. {
  259. _isPositionSliderDragging = false;
  260. //await CurrentPlayer.Seek(Convert.ToInt64(CurrentPositionSlider.Value));
  261. }
  262. /// <summary>
  263. /// Handles the PreviewMouseUp event of the CurrentPositionSlider control.
  264. /// </summary>
  265. /// <param name="sender">The source of the event.</param>
  266. /// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
  267. async void CurrentPositionSlider_PreviewMouseUp(object sender, MouseButtonEventArgs e)
  268. {
  269. await CurrentPlayer.Seek(Convert.ToInt64(CurrentPositionSlider.Value));
  270. }
  271. }
  272. }