school/semester_3/uporabniski_vmesniki/MyApp/MediaController.xaml.cs

90 lines
2.3 KiB
C#

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace MyApp
{
/// <summary>
/// Interaction logic for MediaController.xaml
/// </summary>
public partial class MediaController : UserControl
{
DispatcherTimer timer;
bool isPlaying = false;
public MediaController()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
}
public delegate void TimeChange(object sender, double time);
public event TimeChange? OnTimeChage;
public delegate void MediaLoded(object sender, bool loded, double totalTimeInMilS);
public event MediaLoded? OnMediaLoad;
private void Timer_Tick(object? sender, EventArgs e)
{
OnTimeChage?.Invoke(this, MediaPlayer.Position.TotalSeconds);
}
public bool IsPlaying()
{
return isPlaying;
}
public void Play()
{
timer.Start();
MediaPlayer.Play();
isPlaying = true;
}
public void Stop()
{
timer.Stop();
MediaPlayer.Stop();
isPlaying = false;
OnMediaLoad?.Invoke(this, false, 0);
}
public void Pause()
{
timer.Stop();
MediaPlayer.Pause();
isPlaying = false;
}
public void ChangeTime(double time)
{
MediaPlayer.Position = TimeSpan.FromSeconds(time);
}
public void ChangeContent(string contentAdress)
{
MediaPlayer.Stop();
timer.Stop();
isPlaying = false;
MediaPlayer.Source = new Uri(contentAdress);
OnMediaLoad?.Invoke(this, false, 0);
}
private void MediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
OnMediaLoad?.Invoke(this, true, MediaPlayer.NaturalDuration.TimeSpan.TotalSeconds);
}
private void MediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
timer?.Stop();
MediaPlayer.Stop();
isPlaying = false;
OnMediaLoad?.Invoke(this, false, 0);
}
}
}