Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- CompositionTarget.Rendering += OnCompositionTargetRendering;
- }
- private void SetStart(TimeSpan timeStart)
- {
- x_MediaElement.Position = timeStart;
- }
- private void SetEnd(TimeSpan timeEnd)
- {
- if (x_MediaElement.Markers == null || x_MediaElement.Markers.Count == 0)
- x_MediaElement.Markers.Add(new TimelineMarker() { Time = timeEnd });
- else
- x_MediaElement.Markers[0].Time = timeEnd;
- }
- private bool _InTickEvent;
- private void OnPlayClick(object sender, RoutedEventArgs e)
- {
- SetStart(new TimeSpan(0,0,10));
- SetEnd(new TimeSpan(0,0,20));
- x_MediaElement.Play();
- }
- private void OnPauseClick(object sender, RoutedEventArgs e)
- {
- x_MediaElement.Pause();
- }
- private void OnStopClick(object sender, RoutedEventArgs e)
- {
- x_MediaElement.Stop();
- }
- private void OnMuteClick(object sender, RoutedEventArgs e)
- {
- x_MediaElement.IsMuted = (bool)x_Mute.IsChecked;
- }
- private void OnMediaOpened(object sender, RoutedEventArgs e)
- {
- x_TotalTime.Text = TimeSpanToString(x_MediaElement.NaturalDuration.TimeSpan);
- }
- private void OnMarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
- {
- x_MediaElement.Stop();
- }
- private void OnMediaEnded(object sender, RoutedEventArgs e)
- {
- x_MediaElement.Stop();
- }
- private void OnTimelineValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- if (_InTickEvent)
- return; // throw new Exception("Can't call Seek() now, you'll get an infinite loop");
- double percentComplete = x_Timeline.Value;
- TimeSpan duration = x_MediaElement.NaturalDuration.TimeSpan;
- int newPosition = (int)(duration.TotalSeconds * percentComplete);
- x_MediaElement.Position = new TimeSpan(0, 0, newPosition);
- }
- private void OnCompositionTargetRendering(object sender, EventArgs e)
- {
- _InTickEvent = true;
- TimeSpan duration = x_MediaElement.NaturalDuration.TimeSpan;
- if (duration.TotalSeconds != 0)
- {
- double percentComplete = (x_MediaElement.Position.TotalSeconds / duration.TotalSeconds);
- x_Timeline.Value = percentComplete;
- string text = TimeSpanToString(x_MediaElement.Position);
- if (x_CurrentTime.Text != text)
- x_CurrentTime.Text = text;
- }
- _InTickEvent = false;
- }
- private string TimeSpanToString(TimeSpan time)
- {
- return string.Format("{0:00}:{1:00}", (time.Hours * 60) + time.Minutes, time.Seconds);
- }
- private void OnMediaCurrentStateChanged(object sender, RoutedEventArgs e)
- {
- switch (x_MediaElement.CurrentState)
- {
- case MediaElementState.Buffering:
- break;
- case MediaElementState.Opening:
- break;
- case MediaElementState.Paused:
- break;
- case MediaElementState.Playing:
- break;
- case MediaElementState.Stopped:
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment