VideoPlayer.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. using Microsoft.WindowsAPICodePack.Shell;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Animation;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Windows.Threading;
  18. namespace VideoPlayer
  19. {
  20. public enum VideoState
  21. {
  22. play, stop, pause
  23. }
  24. /// <summary>
  25. /// WenVideoPlayer.xaml 的互動邏輯
  26. /// </summary>
  27. public partial class VideoPlayer : UserControl
  28. {
  29. public event EventHandler OnVideoComplete;
  30. public event EventHandler<VideoState> OnVideoStateChanged;
  31. /// <summary> 取得有總共有多少影片正在撥放 </summary>
  32. /// <return> 撥放中的數量 </return>
  33. public static int playingCount
  34. {
  35. get
  36. {
  37. int count = 0;
  38. foreach (VideoPlayer player in playerCreated)
  39. {
  40. if (player.IsPlaying)
  41. count++;
  42. }
  43. return count;
  44. }
  45. }
  46. public static void ReleaseAll()
  47. {
  48. VideoPlayer[] listClone = new VideoPlayer[playerCreated.Count];
  49. playerCreated.CopyTo(listClone);
  50. foreach (VideoPlayer player in listClone)
  51. {
  52. player.ReleaseResource();
  53. }
  54. }
  55. /// <summary> 阻擋對Player觸控的事件 </summary>
  56. public bool manipulationLocked = false;
  57. private static List<VideoPlayer> playerCreated = new List<VideoPlayer>();
  58. private MediaElement mediaSource;
  59. private bool stopRequest = false;
  60. private bool progressSliderChanging = false;
  61. private DispatcherTimer toolbarHideTimer = new DispatcherTimer();
  62. private const int timerInterval = 3000; //隱藏工具時間
  63. private string _videoPath;
  64. private MediaTimeline mt;
  65. private bool mediaLoadComplete = false;
  66. private VideoState beginBehavier;
  67. private double videoRatio = 0;
  68. private Size videoSize = new Size();
  69. private VideoState currentVideoState;
  70. private double videoLength;
  71. public double VideoLength
  72. {
  73. get
  74. {
  75. return videoLength;
  76. }
  77. }
  78. public bool IsPlaying
  79. {
  80. get
  81. {
  82. return !(mediaSource.Clock.IsPaused || mediaSource.Clock.CurrentState == ClockState.Stopped);
  83. }
  84. }
  85. public Size VideoSize
  86. {
  87. get
  88. {
  89. return videoSize;
  90. }
  91. }
  92. public string VideoPath
  93. {
  94. get
  95. {
  96. return _videoPath;
  97. }
  98. }
  99. public void Stop()
  100. {
  101. stop_TouchDown(null, null);
  102. }
  103. public void Play()
  104. {
  105. uxPlayButton_TouchUp(null, null);
  106. }
  107. public void Pause()
  108. {
  109. pause_TouchDown(null, null);
  110. }
  111. public void IncereaseVolume(double volume = 1)
  112. {
  113. setVolume(uxVolumeSlider.Value + volume);
  114. }
  115. public void DecressVolume(double volume = 1)
  116. {
  117. setVolume(uxVolumeSlider.Value - volume);
  118. }
  119. public void SetVolume(double volume = 1)
  120. {
  121. setVolume(volume);
  122. }
  123. public void UpdateScale(double scale)
  124. {
  125. double ratio = 1;
  126. ((ScaleTransform)uxControlPanel.RenderTransform).ScaleY = 1 / (scale);
  127. uxControlPanel.ColumnDefinitions[1].Width = new GridLength(30 / (scale * ratio), GridUnitType.Pixel);
  128. uxControlPanel.ColumnDefinitions[3].Width = new GridLength(30 / (scale * ratio), GridUnitType.Pixel);
  129. uxControlPanel.ColumnDefinitions[7].Width = new GridLength(30 / (scale * ratio), GridUnitType.Pixel);
  130. uxControlPanel.ColumnDefinitions[0].Width = new GridLength(15 / (scale * ratio), GridUnitType.Pixel);
  131. uxControlPanel.ColumnDefinitions[2].Width = new GridLength(15 / (scale * ratio), GridUnitType.Pixel);
  132. uxControlPanel.ColumnDefinitions[4].Width = new GridLength(15 / (scale * ratio), GridUnitType.Pixel);
  133. uxControlPanel.ColumnDefinitions[6].Width = new GridLength(15 / (scale * ratio), GridUnitType.Pixel);
  134. uxControlPanel.ColumnDefinitions[8].Width = new GridLength(15 / (scale * ratio), GridUnitType.Pixel);
  135. ((ScaleTransform)uxPlayButton.RenderTransform).ScaleX = 1 / scale;
  136. ((ScaleTransform)uxPlayButton.RenderTransform).ScaleY = 1 / scale;
  137. }
  138. public void ReleaseResource()
  139. {
  140. mediaSource.Clock.Completed -= Video_Completed;
  141. mediaSource.MediaOpened -= MediaSource_MediaOpened;
  142. mediaSource.Clock.CurrentTimeInvalidated -= media_CurrentTimeInvalidated;
  143. mediaSource.Clock.Controller.Stop();
  144. mt.Source = null;
  145. mediaSource.Clock = null;
  146. mediaSource.Source = null;
  147. mediaSource = null;
  148. playerCreated.Remove(this);
  149. Console.WriteLine("WenVideoPlayer_ReleaseResource");
  150. }
  151. public FrameworkElement DetatchController()
  152. {
  153. if (uxControlPanel.Parent is Panel parentPanel)
  154. parentPanel.Children.Remove(uxControlPanel);
  155. uxControlPanel.IsVisibleChanged += (s, e) => {
  156. if (uxControlPanel.Visibility == Visibility.Visible)
  157. {
  158. toolbarHideTimer.Stop();
  159. toolbarHideTimer.Start();
  160. }
  161. };
  162. return uxControlPanel;
  163. }
  164. public VideoPlayer(Uri videoPath, VideoState beginBehavier = VideoState.play)
  165. {
  166. InitializeComponent();
  167. currentVideoState = beginBehavier;
  168. //Stylus.SetIsPressAndHoldEnabled(this, false);
  169. //this.IsManipulationEnabled = true;
  170. _videoPath = videoPath.OriginalString;
  171. this.beginBehavier = beginBehavier;
  172. this.Unloaded += WenVideoPlayer_Unloaded;
  173. //this.MouseLeave += WenVideoPlayer_MouseLeave;
  174. //create preview image
  175. ShellFile shellFile = ShellFile.FromFilePath(_videoPath);
  176. ((Image)uxCoverImageParent.Children[0]).Source = shellFile.Thumbnail.ExtraLargeBitmapSource;
  177. //((Image)uxCoverImageParent.Children[0]).Stretch = Stretch.None;
  178. //this.Height = Convert.ToDouble(shellFile.Properties.System.Video.FrameHeight.Value);
  179. //this.Width = Convert.ToDouble(shellFile.Properties.System.Video.FrameWidth.Value);
  180. videoSize.Width = Convert.ToDouble(shellFile.Properties.System.Video.FrameWidth.Value);
  181. videoSize.Height = Convert.ToDouble(shellFile.Properties.System.Video.FrameHeight.Value);
  182. double nanoseconds = -1;
  183. Double.TryParse(shellFile.Properties.System.Media.Duration.Value.ToString(), out nanoseconds);
  184. videoLength = nanoseconds == -1 ? -1 : nanoseconds / 10000000;
  185. this.Loaded += VideoPlayer_Loaded;
  186. //add video player
  187. mediaSource = new MediaElement();
  188. //mediaSource.Height = Convert.ToDouble(shellFile.Properties.System.Video.FrameHeight.Value);
  189. //mediaSource.Width = Convert.ToDouble(shellFile.Properties.System.Video.FrameWidth.Value);
  190. //mediaSource.Stretch = Stretch.Uniform;
  191. mt = new MediaTimeline(videoPath);
  192. mediaSource.Clock = mt.CreateClock();
  193. mediaSource.Clock.CurrentTimeInvalidated += media_CurrentTimeInvalidated;
  194. mediaSource.Clock.Completed += Video_Completed;
  195. mediaSource.LoadedBehavior = MediaState.Manual;
  196. mediaSource.MediaOpened += MediaSource_MediaOpened;
  197. mediaSource.VerticalAlignment = VerticalAlignment.Bottom;
  198. RenderOptions.SetBitmapScalingMode(mediaSource, BitmapScalingMode.HighQuality);
  199. //mediaSource.Margin = new Thickness(0, -10, 0, -10);
  200. toolbarHideTimer.Interval = TimeSpan.FromMilliseconds(timerInterval);
  201. toolbarHideTimer.Tick += AutoHideControlTimer_Tick;
  202. uxVideoPanel.Children.Add(mediaSource);
  203. this.UpdateLayout();
  204. playerCreated.Add(this);
  205. }
  206. private void VideoPlayer_Loaded(object sender, RoutedEventArgs e)
  207. {
  208. //video Height / Width info can get from this.Width & this.Height
  209. //videoRatio = (double)videoSize.Width / videoSize.Height;
  210. //double thisRatio = this.ActualWidth / this.ActualHeight;
  211. //if (videoRatio > thisRatio)
  212. //{
  213. // this.Height = this.ActualWidth / videoRatio;
  214. //}
  215. //else
  216. //{
  217. // this.Width = this.ActualHeight * videoRatio;
  218. //}
  219. uxVideoPanel.Height = this.Height;
  220. uxVideoPanel.Width = this.ActualWidth;
  221. //workaround
  222. mediaSource.Height = this.Height + 6;
  223. mediaSource.Width = this.ActualWidth + 6;
  224. }
  225. private void WenVideoPlayer_MouseLeave(object sender, RoutedEventArgs e)
  226. {
  227. return;
  228. mediaSource.Clock.Controller.Pause();
  229. uxControlPanel.Visibility = Visibility.Collapsed;
  230. uxVolumeGrid.Visibility = Visibility.Collapsed;
  231. uxPlayButtonParent.Visibility = Visibility.Visible;
  232. }
  233. private void WenVideoPlayer_Unloaded(object sender, RoutedEventArgs e)
  234. {
  235. if (mediaSource != null)
  236. {
  237. //mediaSource.Clock.Completed -= Video_Completed;
  238. //mediaSource.MediaOpened -= MediaSource_MediaOpened;
  239. //mediaSource.Clock.CurrentTimeInvalidated -= media_CurrentTimeInvalidated;
  240. mediaSource.Clock.Controller.Stop();
  241. mt.Source = null;
  242. //mediaSource.Clock = null;
  243. mediaSource.Source = null;
  244. //mediaSource = null;
  245. playerCreated.Remove(this);
  246. }
  247. }
  248. private void AutoHideControlTimer_Tick(object sender, EventArgs e)
  249. {
  250. uxControlPanel.Visibility = Visibility.Collapsed;
  251. uxVolumeGrid.Visibility = Visibility.Collapsed;
  252. toolbarHideTimer.Stop();
  253. }
  254. private void Video_Completed(object sender, EventArgs e)
  255. {
  256. mediaSource.Clock.Controller.Stop();
  257. uxControlPanel.Visibility = Visibility.Collapsed;
  258. uxVolumeGrid.Visibility = Visibility.Collapsed;
  259. if (toolbarHideTimer.IsEnabled)
  260. {
  261. toolbarHideTimer.Stop();
  262. }
  263. uxPlayButtonParent.Visibility = Visibility.Visible;
  264. OnVideoComplete?.Invoke(this, new EventArgs());
  265. }
  266. private void MediaSource_MediaOpened(object sender, RoutedEventArgs e)
  267. {
  268. mediaLoadComplete = true;
  269. mediaSource.Volume = uxVolumeSlider.Value / 10;//0.5;
  270. //uxVolumeSlider.Value = 5;
  271. mt.Source.UserInfo.Count();
  272. if (beginBehavier == VideoState.play)
  273. {
  274. Play();
  275. }
  276. else if (beginBehavier == VideoState.stop)
  277. {
  278. Stop();
  279. }
  280. uxCoverImageParent.Visibility = Visibility.Collapsed;
  281. uxPlayButtonParent.Visibility = Visibility.Collapsed;
  282. //WenVideoPlayerIsPlaying += 1;
  283. }
  284. private void media_CurrentTimeInvalidated(object sender, EventArgs e)
  285. {
  286. if (stopRequest == true)
  287. {
  288. mediaSource.Clock.Controller.Stop();
  289. stopRequest = false;
  290. }
  291. if (mediaSource.NaturalDuration.HasTimeSpan && mediaSource.Clock.CurrentTime != null && progressSliderChanging != true)
  292. {
  293. uxProgressSlider.Value = 10 * mediaSource.Clock.CurrentTime.Value.TotalMilliseconds / mediaSource.NaturalDuration.TimeSpan.TotalMilliseconds;
  294. }
  295. }
  296. private void uxVideoPanel_TouchDown(object sender, TouchEventArgs e)
  297. {
  298. //if (uxControlPanel.Visibility == Visibility.Collapsed)
  299. //{
  300. // uxControlPanel.Visibility = Visibility.Visible;
  301. // toolbarHideTimer.Start();
  302. //}
  303. //else if (uxControlPanel.Visibility == Visibility.Visible)
  304. //{
  305. // uxControlPanel.Visibility = Visibility.Collapsed;
  306. // uxVolumeGrid.Visibility = Visibility.Collapsed;
  307. // if (toolbarHideTimer.IsEnabled)
  308. // {
  309. // toolbarHideTimer.Stop();
  310. // }
  311. // uxPlayButtonParent.Visibility = Visibility.Visible;
  312. // mediaSource.Clock.Controller.Pause();
  313. //}
  314. uxControlPanel.Visibility = Visibility.Visible;
  315. toolbarHideTimer.Start();
  316. }
  317. private void pause_TouchDown(object sender, TouchEventArgs e)
  318. {
  319. if (currentVideoState != VideoState.pause)
  320. {
  321. currentVideoState = VideoState.pause;
  322. mediaSource.Clock.Controller.Pause();
  323. uxControlPanel.Visibility = Visibility.Collapsed;
  324. uxVolumeGrid.Visibility = Visibility.Collapsed;
  325. uxPlayButtonParent.Visibility = Visibility.Visible;
  326. OnVideoStateChanged?.Invoke(this, VideoState.pause);
  327. }
  328. }
  329. private void stop_TouchDown(object sender, TouchEventArgs e)
  330. {
  331. if (currentVideoState != VideoState.stop)
  332. {
  333. currentVideoState = VideoState.stop;
  334. if (mediaLoadComplete == true)
  335. {
  336. mediaSource.Clock.Controller.Stop();
  337. uxControlPanel.Visibility = Visibility.Collapsed;
  338. uxVolumeGrid.Visibility = Visibility.Collapsed;
  339. if (toolbarHideTimer.IsEnabled)
  340. {
  341. toolbarHideTimer.Stop();
  342. }
  343. uxPlayButtonParent.Visibility = Visibility.Visible;
  344. uxCoverImageParent.Visibility = Visibility.Visible;
  345. }
  346. else
  347. {
  348. beginBehavier = VideoState.stop;
  349. }
  350. OnVideoStateChanged?.Invoke(this, VideoState.stop);
  351. }
  352. }
  353. private void uxPlayButton_TouchUp(object sender, TouchEventArgs e)
  354. {
  355. if (currentVideoState != VideoState.play)
  356. {
  357. currentVideoState = VideoState.play;
  358. uxPlayButtonParent.Visibility = Visibility.Collapsed;
  359. uxControlPanel.Visibility = Visibility.Collapsed;
  360. uxVolumeGrid.Visibility = Visibility.Collapsed;
  361. if (toolbarHideTimer.IsEnabled)
  362. {
  363. toolbarHideTimer.Stop();
  364. }
  365. if (mediaSource.Clock.CurrentState == ClockState.Stopped)
  366. {
  367. mediaSource.Clock.Controller.Pause();
  368. mediaSource.Clock.Controller.Resume();
  369. mediaSource.Clock.Controller.Begin();
  370. }
  371. else if (mediaSource.Clock.CurrentState == ClockState.Filling)
  372. {
  373. uxCoverImageParent.Visibility = Visibility.Collapsed;
  374. return;
  375. }
  376. if (mediaLoadComplete == false)
  377. {
  378. mediaSource.Clock.Controller.Begin();
  379. beginBehavier = VideoState.play;
  380. return;
  381. }
  382. if (mediaSource.Clock.IsPaused == true)
  383. {
  384. mediaSource.Clock.Controller.Resume();
  385. }
  386. else
  387. {
  388. mediaSource.Clock.Controller.Begin();
  389. }
  390. uxCoverImageParent.Visibility = Visibility.Collapsed;
  391. OnVideoStateChanged?.Invoke(this, VideoState.play);
  392. }
  393. }
  394. private void uxProgressSlider_TouchDown(object sender, TouchEventArgs e)
  395. {
  396. progressSliderChanging = true;
  397. mediaSource.Clock.Controller.Pause();
  398. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  399. uxProgressSlider.Value = uxProgressSlider.Maximum * (pt.X) / uxProgressSlider.ActualWidth;
  400. e.Handled = true;
  401. Console.WriteLine("uxProgressSlider_TouchDown");
  402. }
  403. private void uxProgressSlider_TouchUp(object sender, TouchEventArgs e)
  404. {
  405. progressSliderChanging = false;
  406. mediaSource.Clock.Controller.Resume();
  407. }
  408. private void uxProgressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  409. {
  410. if (progressSliderChanging == true)
  411. {
  412. mediaSource.Clock.Controller.SeekAlignedToLastTick(new TimeSpan((long)(mediaSource.NaturalDuration.TimeSpan.Ticks * uxProgressSlider.Value / 10)), new System.Windows.Media.Animation.TimeSeekOrigin());
  413. }
  414. }
  415. private void uxProgressSlider_TouchMove(object sender, TouchEventArgs e)
  416. {
  417. progressSliderChanging = true;
  418. if (mediaSource.Clock.IsPaused == false)
  419. {
  420. mediaSource.Clock.Controller.Pause();
  421. }
  422. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  423. uxProgressSlider.Value = uxProgressSlider.Maximum * (pt.X) / uxProgressSlider.ActualWidth;
  424. e.Handled = true;
  425. }
  426. private void Global_TouchUp(object sender, TouchEventArgs e)
  427. {
  428. if (progressSliderChanging == true)
  429. {
  430. progressSliderChanging = false;
  431. mediaSource.Clock.Controller.Resume();
  432. }
  433. if (e.Source is Grid && ((Grid)e.Source) == uxPlayButtonParent)
  434. {
  435. uxPlayButton_TouchUp(sender, e);
  436. }
  437. else if (toolbarHideTimer.IsEnabled == true)
  438. {
  439. toolbarHideTimer.Stop();
  440. toolbarHideTimer.Start();
  441. }
  442. }
  443. private void uxSound_TouchDown(object sender, TouchEventArgs e)
  444. {
  445. if (uxVolumeGrid.Visibility == Visibility.Collapsed)
  446. {
  447. uxVolumeGrid.Visibility = Visibility.Visible;
  448. ((Storyboard)FindResource("sbShowVolSlider")).Begin();
  449. }
  450. else
  451. {
  452. Storyboard sbBuf = ((Storyboard)FindResource("sbHideVolSlider"));
  453. sbBuf.Completed += delegate (object s, EventArgs se)
  454. {
  455. uxVolumeGrid.Visibility = Visibility.Collapsed;
  456. };
  457. sbBuf.Begin();
  458. }
  459. }
  460. private void uxVolumeGrid_TouchDown(object sender, TouchEventArgs e)
  461. {
  462. e.Handled = true;
  463. }
  464. private void uxVolumeGrid_TouchMove(object sender, TouchEventArgs e)
  465. {
  466. e.Handled = true;
  467. }
  468. private void uxVolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  469. {
  470. setVolume(e.NewValue);
  471. }
  472. private void setVolume(double volume)
  473. {
  474. if (uxVolumeSlider.Value != volume)
  475. {
  476. uxVolumeSlider.Value = volume;
  477. if (volume == 0)
  478. {
  479. uxNoSound.Visibility = Visibility.Visible;
  480. uxSound.Visibility = Visibility.Collapsed;
  481. }
  482. else if (uxNoSound.Visibility == Visibility.Visible) //and e.NewValue != 0
  483. {
  484. uxNoSound.Visibility = Visibility.Collapsed;
  485. uxSound.Visibility = Visibility.Visible;
  486. }
  487. }
  488. if (mediaSource != null && mediaSource.IsLoaded)
  489. {
  490. mediaSource.Volume = volume / 10;
  491. }
  492. }
  493. private void ResetTimer(object sender, TouchEventArgs e)
  494. {
  495. if (toolbarHideTimer.IsEnabled == true)
  496. {
  497. toolbarHideTimer.Stop();
  498. toolbarHideTimer.Start();
  499. }
  500. }
  501. private void Grid_PreviewTouchUp(object sender, TouchEventArgs e)
  502. {
  503. if (manipulationLocked == true)
  504. {
  505. e.Handled = true;
  506. return;
  507. }
  508. }
  509. private void uxVolumeSlider_TouchDown(object sender, TouchEventArgs e)
  510. {
  511. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  512. uxVolumeSlider.Value = uxVolumeSlider.Maximum * (uxVolumeSlider.ActualHeight - pt.Y) / uxVolumeSlider.ActualHeight;
  513. e.Handled = true;
  514. }
  515. private void uxVolumeSlider_TouchMove(object sender, TouchEventArgs e)
  516. {
  517. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  518. uxVolumeSlider.Value = uxVolumeSlider.Maximum * (uxVolumeSlider.ActualHeight - pt.Y) / uxVolumeSlider.ActualHeight;
  519. //e.Handled = true;
  520. }
  521. }
  522. }