media player

main
syneffort 2 years ago
parent dd8bb2667c
commit 26e8f9ceb4
  1. 9
      PacticeSolution/MediaPlayer/App.xaml
  2. 17
      PacticeSolution/MediaPlayer/App.xaml.cs
  3. 10
      PacticeSolution/MediaPlayer/AssemblyInfo.cs
  4. 54
      PacticeSolution/MediaPlayer/MainWindow.xaml
  5. 122
      PacticeSolution/MediaPlayer/MainWindow.xaml.cs
  6. 10
      PacticeSolution/MediaPlayer/MediaPlayer.csproj
  7. 14
      PacticeSolution/PacticeSolution.sln

@ -0,0 +1,9 @@
<Application x:Class="MediaPlayer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MediaPlayer"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace MediaPlayer
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,54 @@
<Window x:Class="MediaPlayer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MediaPlayer"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<MediaElement x:Name="mediaElem" Grid.Row="0"
LoadedBehavior="Manual" UnloadedBehavior="Stop"
MediaOpened="mediaElem_MediaOpened"
MediaEnded="mediaElem_MediaEnded"
MediaFailed="mediaElem_MediaFailed"/>
<Slider x:Name="playSlider" Grid.Row="1" Margin="20 0 20 0" VerticalAlignment="Center" HorizontalAlignment="Stretch"
Thumb.DragStarted="playSlider_DragStarted"
Thumb.DragCompleted="playSlider_DragCompleted"
ValueChanged="playSlider_ValueChanged"/>
<Grid Grid.Row="2" Margin="20 0 20 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal">
<Button x:Name="playButton" Width="50" Height="30" Margin="5" Content="Play"
Click="playButton_Click"/>
<Button x:Name="PauseButton" Width="50" Height="30" Margin="5" Content="Pause"
Click="PauseButton_Click"/>
<Button x:Name="StopButton" Width="50" Height="30" Margin="5" Content="Stop"
Click="StopButton_Click"/>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Label Content="Volume" VerticalAlignment="Center"/>
<Slider x:Name="volumeSlider" Width="100" VerticalAlignment="Center"
Maximum="1" LargeChange="0.1" Value="0.5"
ValueChanged="volumeSlider_ValueChanged"/>
</StackPanel>
<Label x:Name="playTimeLabel" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"
Content="00:00:00 / 00:00:00"/>
<Button x:Name="openButton" Grid.Column="4" Width="50" Height="30" HorizontalAlignment="Right" Content="Open"
Click="openButton_Click"/>
</Grid>
</Grid>
</Window>

@ -0,0 +1,122 @@
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")}";
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -117,13 +117,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComboBoxSample", "ComboBoxS
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObervableCollectionSample", "ObervableCollectionSample\ObervableCollectionSample.csproj", "{0C2F6C56-5CAB-44BB-9890-7512C944CE1D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataTemplateSample", "DataTemplateSample\DataTemplateSample.csproj", "{D6507C41-143B-45D2-9A45-F822A9E22B7E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataTemplateSample", "DataTemplateSample\DataTemplateSample.csproj", "{D6507C41-143B-45D2-9A45-F822A9E22B7E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandSample", "CommandSample\CommandSample.csproj", "{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandSample", "CommandSample\CommandSample.csproj", "{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqInNotIn", "LinqInNotIn\LinqInNotIn.csproj", "{9261D8E1-5DEE-4329-AB32-946AD5E69C66}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqInNotIn", "LinqInNotIn\LinqInNotIn.csproj", "{9261D8E1-5DEE-4329-AB32-946AD5E69C66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkSample", "EntityFrameworkSample\EntityFrameworkSample.csproj", "{AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkSample", "EntityFrameworkSample\EntityFrameworkSample.csproj", "{AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaPlayer", "MediaPlayer\MediaPlayer.csproj", "{0E7DB4A2-B598-452B-B1B4-CF36685D0D53}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -375,6 +377,10 @@ Global
{AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAEF47BF-A410-4A4D-942B-D9A1F4AA1343}.Release|Any CPU.Build.0 = Release|Any CPU
{0E7DB4A2-B598-452B-B1B4-CF36685D0D53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E7DB4A2-B598-452B-B1B4-CF36685D0D53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E7DB4A2-B598-452B-B1B4-CF36685D0D53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E7DB4A2-B598-452B-B1B4-CF36685D0D53}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save