You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5 KiB

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MediaPlayer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DispatcherTimer _playTimer;
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
_playTimer = new DispatcherTimer();
_playTimer.Interval = TimeSpan.FromSeconds(1);
_playTimer.Tick -= TickTimer;
_playTimer.Tick += TickTimer;
_playTimer.Start();
}
private void mediaElem_MediaOpened(object sender, RoutedEventArgs e)
{
playSlider.Minimum = 0;
playSlider.Maximum = mediaElem.NaturalDuration.TimeSpan.TotalSeconds;
}
private void mediaElem_MediaEnded(object sender, RoutedEventArgs e)
{
mediaElem.Stop();
}
private void mediaElem_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show($"Play error: {e.ErrorException.Message}");
}
private void playSlider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mediaElem.Pause();
}
private void playSlider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mediaElem.Position = TimeSpan.FromSeconds(playSlider.Value);
mediaElem.Play();
}
private void playSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mediaElem.Position = TimeSpan.FromSeconds(playSlider.Value);
}
private void playButton_Click(object sender, RoutedEventArgs e)
{
mediaElem.Play();
}
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
mediaElem.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
mediaElem.Stop();
}
private void volumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mediaElem.Volume = volumeSlider.Value;
}
private void openButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() != true)
return;
mediaElem.Source = new Uri(dlg.FileName);
mediaElem.Volume = volumeSlider.Value;
mediaElem.SpeedRatio = 1;
mediaElem.Play();
}
private void TickTimer(object? sender, EventArgs e)
{
if (mediaElem.Source == null)
{
playTimeLabel.Content = "No File";
return;
}
if (!mediaElem.NaturalDuration.HasTimeSpan)
return;
playSlider.Value = mediaElem.Position.TotalSeconds;
playTimeLabel.Content = $"{mediaElem.Position.ToString(@"hh\:mm\:ss")} / {mediaElem.NaturalDuration.TimeSpan.ToString(@"hh\:mm\:ss")}";
}
}
}