Initial commit
This commit is contained in:
98
TechMedia.Interface.Wpf/MainWindow.xaml.cs
Normal file
98
TechMedia.Interface.Wpf/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
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 TechMedia.Backend.Vlc;
|
||||
using TechMedia.Core;
|
||||
using TechMedia.Core.Model;
|
||||
|
||||
namespace TechMedia.Interface.Wpf
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public Track CurrentTrack { get; set; } = new();
|
||||
private readonly ITechMediaCore _player = new TechMediaCore(new VlcAudioBackend());
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = CurrentTrack;
|
||||
_player.RegisterPlaylistChangeListener(new Progress<List<Track>>(newPlaylist => TrackDataGrid.DataContext = newPlaylist));
|
||||
}
|
||||
|
||||
private static string FormatMillis(int millis)
|
||||
{
|
||||
var seconds = millis / 1000;
|
||||
return $"{seconds / 60}:{(seconds % 60).ToString().PadLeft(2, '0')}";
|
||||
}
|
||||
|
||||
private void PlayButtonClicked(object sender, EventArgs e)
|
||||
{
|
||||
var trackUpdater = new Progress<Track>(track =>
|
||||
{
|
||||
CurrentTrack = track;
|
||||
TitleLabel.Content = track.Title ?? "No title available";
|
||||
AlbumLabel.Content = track.Album ?? "No album available";
|
||||
});
|
||||
var progressUpdater = new Progress<float>(p =>
|
||||
{
|
||||
PlaybackProgressBar.Value = p;
|
||||
TimeLabel.Content = $"{FormatMillis((int)(CurrentTrack.Duration * p))}/{FormatMillis((int)(CurrentTrack.Duration))}";
|
||||
});
|
||||
PlaybackProgressBar.Maximum = 1.0f;
|
||||
_player.StartPlaylistPlayback(trackUpdater, progressUpdater);
|
||||
}
|
||||
|
||||
private async void LoadButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var allFiles = Directory.EnumerateFiles(FilenameTextBox.Text, "*.*", SearchOption.AllDirectories).ToList();
|
||||
LoadProgressBar.Maximum = allFiles.Count;
|
||||
IProgress<int> progress = new Progress<int>(state =>
|
||||
{
|
||||
LoadProgressBar.Value = state;
|
||||
LoadingTextBlock.Text = $"Loading... {state}/{allFiles.Count}";
|
||||
});
|
||||
var loadedCount = await _player.LoadFiles(allFiles, progress);
|
||||
LoadingTextBlock.Text = $"Tracks loaded: {loadedCount}";
|
||||
LoadProgressBar.Value = LoadProgressBar.Maximum;
|
||||
MessageBox.Show($"Loaded {loadedCount} tracks");
|
||||
TrackDataGrid.DataContext = _player.GetCurrentPlaylist();
|
||||
}
|
||||
|
||||
private void ShuffleButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
TrackDataGrid.DataContext = _player.ShuffleCurrentPlaylist();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_player.Stop();
|
||||
}
|
||||
|
||||
private void Button_Click_1(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var requestedTrack = _player.SubmitRequest(RequestFilenameTextBox.Text);
|
||||
if (requestedTrack != null)
|
||||
{
|
||||
RequestStatusLabel.Content = $"{requestedTrack.Title} added to queue";
|
||||
}
|
||||
else
|
||||
{
|
||||
RequestStatusLabel.Content = "File not found in playlist";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user