Опубликован: 15.06.2011 | Доступ: свободный | Студентов: 708 / 59 | Оценка: 4.25 / 4.00 | Длительность: 14:09:00
Специальности: Программист, Разработчик интернет-проектов
Теги:
Самостоятельная работа 14:
Разработка простого медиа - плеера в среде Visual Studio 2010 используя Silverlight
26.И в заключении, листинг кода (файл MainPageXaml.cs ):
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Threading; namespace mediaplayer { public partial class MainPage : UserControl { DispatcherTimer VideoTimer; public MainPage() { InitializeComponent(); } private void PlayButton_Click(object sender, RoutedEventArgs e) { if (mediaElement1.CurrentState == MediaElementState.Paused) { mediaElement1.Play(); } else { mediaElement1.Position = new TimeSpan(0); mediaElement1.Play(); } } private void PauseButton_Click(object sender, RoutedEventArgs e) { if (MediaElementState.Paused == mediaElement1.CurrentState) { mediaElement1.Play(); } else { mediaElement1.Pause(); } } private void StopButton_Click(object sender, RoutedEventArgs e) { mediaElement1.Stop(); } private void VolumeSlider_ValueChanged (object sender, RoutedPropertyChangedEventArgs<double> e) { mediaElement1.Volume = VolumeSlider.Value; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { VolumeSlider.Value = 1; VolumeSlider.Minimum = 0; VolumeSlider.Maximum = 1; PositionSlider.Value = 0; PositionSlider.Minimum = 0; PositionSlider.Maximum = 1; PositionSlider.LargeChange = 0.10; PositionSlider.SmallChange = 0.01; VideoTimer = new DispatcherTimer(); VideoTimer.Interval = TimeSpan.FromMilliseconds(50); VideoTimer.Tick += new EventHandler(VideoTimer_Tick); } private void PositionSlider_MouseLeftButtonUp (object sender, MouseButtonEventArgs e) { mediaElement1.Position = TimeSpan.FromSeconds(Convert.ToInt64 (PositionSlider.Value * mediaElement1.NaturalDuration.TimeSpan.TotalSeconds)); Timer.Text = string.Format("{0:00}:{1:00}", mediaElement1.Position.Minutes, mediaElement1.Position.Seconds); mediaElement1.Play(); } private void PositionSlider_MouseLeftButtonDown (object sender, MouseButtonEventArgs e) { mediaElement1.Pause(); } void VideoTimer_Tick(object sender, EventArgs e) { if (mediaElement1.NaturalDuration.TimeSpan.TotalSeconds > 0) { Timer.Text = string.Format("{0:00}:{1:00}", mediaElement1.Position.Minutes, mediaElement1.Position.Seconds); PositionSlider.Value = mediaElement1.Position.TotalSeconds / mediaElement1.NaturalDuration.TimeSpan.TotalSeconds; } } private void mediaElement1_CurrentStateChanged(object sender, RoutedEventArgs e) { if (mediaElement1.CurrentState == MediaElementState.Playing) { VideoTimer.Start(); } else { VideoTimer.Stop(); } } } }