VideoPlayer.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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.Stop;
  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. mediaSource.Clock.Controller.Stop();
  279. Stop();
  280. }
  281. uxCoverImageParent.Visibility = Visibility.Collapsed;
  282. uxPlayButtonParent.Visibility = Visibility.Collapsed;
  283. //WenVideoPlayerIsPlaying += 1;
  284. }
  285. private void media_CurrentTimeInvalidated(object sender, EventArgs e)
  286. {
  287. if (stopRequest == true)
  288. {
  289. mediaSource.Clock.Controller.Stop();
  290. stopRequest = false;
  291. }
  292. if (mediaSource.NaturalDuration.HasTimeSpan && mediaSource.Clock.CurrentTime != null && progressSliderChanging != true)
  293. {
  294. uxProgressSlider.Value = 10 * mediaSource.Clock.CurrentTime.Value.TotalMilliseconds / mediaSource.NaturalDuration.TimeSpan.TotalMilliseconds;
  295. }
  296. }
  297. private void uxVideoPanel_TouchDown(object sender, TouchEventArgs e)
  298. {
  299. //if (uxControlPanel.Visibility == Visibility.Collapsed)
  300. //{
  301. // uxControlPanel.Visibility = Visibility.Visible;
  302. // toolbarHideTimer.Start();
  303. //}
  304. //else if (uxControlPanel.Visibility == Visibility.Visible)
  305. //{
  306. // uxControlPanel.Visibility = Visibility.Collapsed;
  307. // uxVolumeGrid.Visibility = Visibility.Collapsed;
  308. // if (toolbarHideTimer.IsEnabled)
  309. // {
  310. // toolbarHideTimer.Stop();
  311. // }
  312. // uxPlayButtonParent.Visibility = Visibility.Visible;
  313. // mediaSource.Clock.Controller.Pause();
  314. //}
  315. uxControlPanel.Visibility = Visibility.Visible;
  316. toolbarHideTimer.Start();
  317. }
  318. private void pause_TouchDown(object sender, TouchEventArgs e)
  319. {
  320. if (currentVideoState != VideoState.pause)
  321. {
  322. currentVideoState = VideoState.pause;
  323. mediaSource.Clock.Controller.Pause();
  324. uxControlPanel.Visibility = Visibility.Collapsed;
  325. uxVolumeGrid.Visibility = Visibility.Collapsed;
  326. uxPlayButtonParent.Visibility = Visibility.Visible;
  327. OnVideoStateChanged?.Invoke(this, VideoState.pause);
  328. }
  329. }
  330. private void stop_TouchDown(object sender, TouchEventArgs e)
  331. {
  332. if (currentVideoState != VideoState.stop)
  333. {
  334. currentVideoState = VideoState.stop;
  335. if (mediaLoadComplete == true)
  336. {
  337. mediaSource.Clock.Controller.Stop();
  338. uxControlPanel.Visibility = Visibility.Collapsed;
  339. uxVolumeGrid.Visibility = Visibility.Collapsed;
  340. if (toolbarHideTimer.IsEnabled)
  341. {
  342. toolbarHideTimer.Stop();
  343. }
  344. uxPlayButtonParent.Visibility = Visibility.Visible;
  345. uxCoverImageParent.Visibility = Visibility.Visible;
  346. }
  347. else
  348. {
  349. beginBehavier = VideoState.stop;
  350. }
  351. OnVideoStateChanged?.Invoke(this, VideoState.stop);
  352. }
  353. }
  354. private void uxPlayButton_TouchUp(object sender, TouchEventArgs e)
  355. {
  356. if (currentVideoState != VideoState.play)
  357. {
  358. currentVideoState = VideoState.play;
  359. uxPlayButtonParent.Visibility = Visibility.Collapsed;
  360. uxControlPanel.Visibility = Visibility.Collapsed;
  361. uxVolumeGrid.Visibility = Visibility.Collapsed;
  362. if (toolbarHideTimer.IsEnabled)
  363. {
  364. toolbarHideTimer.Stop();
  365. }
  366. if (mediaSource.Clock.CurrentState == ClockState.Stopped)
  367. {
  368. mediaSource.Clock.Controller.Pause();
  369. mediaSource.Clock.Controller.Resume();
  370. mediaSource.Clock.Controller.Begin();
  371. }
  372. else if (mediaSource.Clock.CurrentState == ClockState.Filling)
  373. {
  374. uxCoverImageParent.Visibility = Visibility.Collapsed;
  375. return;
  376. }
  377. if (mediaLoadComplete == false)
  378. {
  379. mediaSource.Clock.Controller.Begin();
  380. beginBehavier = VideoState.play;
  381. return;
  382. }
  383. if (mediaSource.Clock.IsPaused == true)
  384. {
  385. mediaSource.Clock.Controller.Resume();
  386. }
  387. else
  388. {
  389. mediaSource.Clock.Controller.Begin();
  390. }
  391. uxCoverImageParent.Visibility = Visibility.Collapsed;
  392. OnVideoStateChanged?.Invoke(this, VideoState.play);
  393. }
  394. }
  395. private void uxProgressSlider_TouchDown(object sender, TouchEventArgs e)
  396. {
  397. progressSliderChanging = true;
  398. mediaSource.Clock.Controller.Pause();
  399. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  400. uxProgressSlider.Value = uxProgressSlider.Maximum * (pt.X) / uxProgressSlider.ActualWidth;
  401. e.Handled = true;
  402. Console.WriteLine("uxProgressSlider_TouchDown");
  403. }
  404. private void uxProgressSlider_TouchUp(object sender, TouchEventArgs e)
  405. {
  406. progressSliderChanging = false;
  407. mediaSource.Clock.Controller.Resume();
  408. }
  409. private void uxProgressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  410. {
  411. if (progressSliderChanging == true)
  412. {
  413. mediaSource.Clock.Controller.SeekAlignedToLastTick(new TimeSpan((long)(mediaSource.NaturalDuration.TimeSpan.Ticks * uxProgressSlider.Value / 10)), new System.Windows.Media.Animation.TimeSeekOrigin());
  414. }
  415. }
  416. private void uxProgressSlider_TouchMove(object sender, TouchEventArgs e)
  417. {
  418. progressSliderChanging = true;
  419. if (mediaSource.Clock.IsPaused == false)
  420. {
  421. mediaSource.Clock.Controller.Pause();
  422. }
  423. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  424. uxProgressSlider.Value = uxProgressSlider.Maximum * (pt.X) / uxProgressSlider.ActualWidth;
  425. e.Handled = true;
  426. }
  427. private void Global_TouchUp(object sender, TouchEventArgs e)
  428. {
  429. if (progressSliderChanging == true)
  430. {
  431. progressSliderChanging = false;
  432. mediaSource.Clock.Controller.Resume();
  433. }
  434. if (e.Source is Grid && ((Grid)e.Source) == uxPlayButtonParent)
  435. {
  436. uxPlayButton_TouchUp(sender, e);
  437. }
  438. else if (toolbarHideTimer.IsEnabled == true)
  439. {
  440. toolbarHideTimer.Stop();
  441. toolbarHideTimer.Start();
  442. }
  443. }
  444. private void uxSound_TouchDown(object sender, TouchEventArgs e)
  445. {
  446. if (uxVolumeGrid.Visibility == Visibility.Collapsed)
  447. {
  448. uxVolumeGrid.Visibility = Visibility.Visible;
  449. ((Storyboard)FindResource("sbShowVolSlider")).Begin();
  450. }
  451. else
  452. {
  453. Storyboard sbBuf = ((Storyboard)FindResource("sbHideVolSlider"));
  454. sbBuf.Completed += delegate (object s, EventArgs se)
  455. {
  456. uxVolumeGrid.Visibility = Visibility.Collapsed;
  457. };
  458. sbBuf.Begin();
  459. }
  460. }
  461. private void uxVolumeGrid_TouchDown(object sender, TouchEventArgs e)
  462. {
  463. e.Handled = true;
  464. }
  465. private void uxVolumeGrid_TouchMove(object sender, TouchEventArgs e)
  466. {
  467. e.Handled = true;
  468. }
  469. private void uxVolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  470. {
  471. setVolume(e.NewValue);
  472. }
  473. private void setVolume(double volume)
  474. {
  475. if (uxVolumeSlider.Value != volume)
  476. {
  477. uxVolumeSlider.Value = volume;
  478. if (volume == 0)
  479. {
  480. uxNoSound.Visibility = Visibility.Visible;
  481. uxSound.Visibility = Visibility.Collapsed;
  482. }
  483. else if (uxNoSound.Visibility == Visibility.Visible) //and e.NewValue != 0
  484. {
  485. uxNoSound.Visibility = Visibility.Collapsed;
  486. uxSound.Visibility = Visibility.Visible;
  487. }
  488. }
  489. if (mediaSource != null && mediaSource.IsLoaded)
  490. {
  491. mediaSource.Volume = volume / 10;
  492. }
  493. }
  494. private void ResetTimer(object sender, TouchEventArgs e)
  495. {
  496. if (toolbarHideTimer.IsEnabled == true)
  497. {
  498. toolbarHideTimer.Stop();
  499. toolbarHideTimer.Start();
  500. }
  501. }
  502. private void Grid_PreviewTouchUp(object sender, TouchEventArgs e)
  503. {
  504. if (manipulationLocked == true)
  505. {
  506. e.Handled = true;
  507. return;
  508. }
  509. }
  510. private void uxVolumeSlider_TouchDown(object sender, TouchEventArgs e)
  511. {
  512. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  513. uxVolumeSlider.Value = uxVolumeSlider.Maximum * (uxVolumeSlider.ActualHeight - pt.Y) / uxVolumeSlider.ActualHeight;
  514. e.Handled = true;
  515. }
  516. private void uxVolumeSlider_TouchMove(object sender, TouchEventArgs e)
  517. {
  518. Point pt = e.GetTouchPoint((UIElement)sender).Position;
  519. uxVolumeSlider.Value = uxVolumeSlider.Maximum * (uxVolumeSlider.ActualHeight - pt.Y) / uxVolumeSlider.ActualHeight;
  520. //e.Handled = true;
  521. }
  522. }
  523. }