Guest User

Untitled

a guest
Oct 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. public partial class MainPage : UserControl
  2. {
  3. public MainPage()
  4. {
  5. InitializeComponent();
  6. CompositionTarget.Rendering += OnCompositionTargetRendering;
  7. }
  8.  
  9. private void SetStart(TimeSpan timeStart)
  10. {
  11. x_MediaElement.Position = timeStart;
  12. }
  13.  
  14. private void SetEnd(TimeSpan timeEnd)
  15. {
  16. if (x_MediaElement.Markers == null || x_MediaElement.Markers.Count == 0)
  17. x_MediaElement.Markers.Add(new TimelineMarker() { Time = timeEnd });
  18. else
  19. x_MediaElement.Markers[0].Time = timeEnd;
  20. }
  21.  
  22. private bool _InTickEvent;
  23.  
  24. private void OnPlayClick(object sender, RoutedEventArgs e)
  25. {
  26. SetStart(new TimeSpan(0,0,10));
  27. SetEnd(new TimeSpan(0,0,20));
  28. x_MediaElement.Play();
  29. }
  30.  
  31. private void OnPauseClick(object sender, RoutedEventArgs e)
  32. {
  33. x_MediaElement.Pause();
  34. }
  35.  
  36. private void OnStopClick(object sender, RoutedEventArgs e)
  37. {
  38. x_MediaElement.Stop();
  39. }
  40.  
  41. private void OnMuteClick(object sender, RoutedEventArgs e)
  42. {
  43. x_MediaElement.IsMuted = (bool)x_Mute.IsChecked;
  44. }
  45.  
  46. private void OnMediaOpened(object sender, RoutedEventArgs e)
  47. {
  48. x_TotalTime.Text = TimeSpanToString(x_MediaElement.NaturalDuration.TimeSpan);
  49. }
  50.  
  51. private void OnMarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
  52. {
  53. x_MediaElement.Stop();
  54. }
  55.  
  56. private void OnMediaEnded(object sender, RoutedEventArgs e)
  57. {
  58. x_MediaElement.Stop();
  59. }
  60.  
  61. private void OnTimelineValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  62. {
  63. if (_InTickEvent)
  64. return; // throw new Exception("Can't call Seek() now, you'll get an infinite loop");
  65.  
  66. double percentComplete = x_Timeline.Value;
  67. TimeSpan duration = x_MediaElement.NaturalDuration.TimeSpan;
  68. int newPosition = (int)(duration.TotalSeconds * percentComplete);
  69. x_MediaElement.Position = new TimeSpan(0, 0, newPosition);
  70. }
  71.  
  72. private void OnCompositionTargetRendering(object sender, EventArgs e)
  73. {
  74. _InTickEvent = true;
  75.  
  76. TimeSpan duration = x_MediaElement.NaturalDuration.TimeSpan;
  77. if (duration.TotalSeconds != 0)
  78. {
  79. double percentComplete = (x_MediaElement.Position.TotalSeconds / duration.TotalSeconds);
  80. x_Timeline.Value = percentComplete;
  81. string text = TimeSpanToString(x_MediaElement.Position);
  82. if (x_CurrentTime.Text != text)
  83. x_CurrentTime.Text = text;
  84. }
  85.  
  86. _InTickEvent = false;
  87. }
  88.  
  89. private string TimeSpanToString(TimeSpan time)
  90. {
  91. return string.Format("{0:00}:{1:00}", (time.Hours * 60) + time.Minutes, time.Seconds);
  92. }
  93.  
  94. private void OnMediaCurrentStateChanged(object sender, RoutedEventArgs e)
  95. {
  96. switch (x_MediaElement.CurrentState)
  97. {
  98. case MediaElementState.Buffering:
  99. break;
  100. case MediaElementState.Opening:
  101. break;
  102. case MediaElementState.Paused:
  103. break;
  104. case MediaElementState.Playing:
  105. break;
  106. case MediaElementState.Stopped:
  107. break;
  108. }
  109. }
  110. }
Add Comment
Please, Sign In to add comment