WPF 播放器
前台代码:
<Window
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"
x:Class="MusicPlayer.Window1"
xmlns:Converters ="clr-namespace:MusicPlayer"
WindowStyle="None" ResizeMode="NoResize"
Title="Window1" Height="240" Width="402" mc:Ignorable="d" Background="#FF554D4A">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="White"/>
<GradientStop Offset="0.5" Color="#FF554D4A"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Foreground" Value="Gold"/>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Height="240" Background="#FF554D4A" Margin="0,5,0,-5" d:LayoutOverrides="HorizontalMargin">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="30"/>
<RowDefinition Height="45"/>
</Grid.RowDefinitions>
<MediaElement x:Name="mdeMusic"
IsMuted="False" Stretch="Fill" Volume="0.5"
MediaEnded="mdeMusic_MediaEnded"
MediaOpened="mdeMusic_MediaOpened"
LoadedBehavior="Manual"
Grid.Row="0"
UnloadedBehavior="Stop"/>
<Slider Minimum="0" ValueChanged="sldProgress_ValueChanged"
x:Name="sldProgress" Grid.Row="1" VerticalAlignment="Bottom"/>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="75"/>
<ColumnDefinition />
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" x:Name="btnPlay" Content="Play" Click="btnPlay_Click"></Button>
<Button Grid.Column="1" x:Name="btnStop" Content="Stop" Click="btnStop_Click" ></Button>
<Button Grid.Column="2" x:Name="btnOpen" Content="Open" Click="btnOpen_Click" HorizontalAlignment="Right" d:LayoutOverrides="Width"></Button>
<Slider Value="{Binding Volume, ElementName=mdeMusic, Mode=TwoWay, UpdateSourceTrigger=Default}"
VerticalAlignment="Center"
Maximum="1" LargeChange="0.1" SmallChange="0.01" x:Name="sldVolumn" Grid.Column="4" />
</Grid>
<TextBlock x:Name="txtMusicName" TextWrapping="Wrap" Margin="0,-3,0,0" VerticalAlignment="Top" Height="25" />
</Grid>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Microsoft.Win32;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace MusicPlayer
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
OpenFileDialog openFile = new OpenFileDialog();
private string videoName = "";
private DispatcherTimer timer;
public Window1()
{
InitializeComponent();
this.timer = new DispatcherTimer();
}
/// <summary>
/// 打开文件时 设置进度条最大值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mdeMusic_MediaOpened(object sender, System.Windows.RoutedEventArgs e)
{
double seconds = mdeMusic.NaturalDuration.TimeSpan.TotalSeconds;
sldProgress.Maximum = seconds / 10;
sldProgress.Value = 0;
//一个所代表的秒数
double baseSecond = seconds / sldProgress.Maximum;
this.timer.Interval = new TimeSpan(0, 0, 1);
this.timer.Tick += new EventHandler(timer_Tick);
this.timer.Start();
}
/// <summary>
/// 播放结束时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mdeMusic_MediaEnded(object sender, RoutedEventArgs e)
{
btnPlay.Content = "Play";
mdeMusic.Position = TimeSpan.Zero;
mdeMusic.Pause();
}
private void btnPlay_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (this.btnPlay.Content.ToString() == "Play")
{
this.btnPlay.Content = "Pause";
mdeMusic.Play();
txtMusicName.Text = "正在播放:" + openFile.SafeFileName;
InitializePropertyValues();
}
else
{
mdeMusic.Pause();
txtMusicName.Text = "已暂停:" + openFile.SafeFileName;
this.btnPlay.Content = "Play";
}
}
/// <summary>
///
/// </summary>
public void UserControl1()
{
InitializeComponent();
Timeline.DesiredFrameRateProperty.OverrideMetadata(
typeof(Timeline),
new FrameworkPropertyMetadata
{
DefaultValue = 20
});
}
private void btnStop_Click(object sender, System.Windows.RoutedEventArgs e)
{
mdeMusic.Position = TimeSpan.Zero;
this.txtMusicName.Text = "";
btnPlay.Content = "Play";
mdeMusic.Stop();
}
/// <summary>
/// 打开音乐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click(object sender, System.Windows.RoutedEventArgs e)
{
openFile.Filter = "All Files(*.*)|*.*";
openFile.InitialDirectory = "F:\\films";
openFile.ShowDialog(this);
videoName = openFile.FileName;
if ((videoName != null) && (videoName != ""))
{
Uri file = new Uri(videoName);
mdeMusic.Source = file;
mdeMusic.LoadedBehavior = MediaState.Manual;
mdeMusic.Play();
}
}
void InitializePropertyValues()
{
mdeMusic.Volume = (double)sldVolumn.Value;
}
private void sldProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
TimeSpan span = new TimeSpan(0, 0, (int)(sldProgress.Value) * 10);
mdeMusic.Position = span;
}
void timer_Tick(object sender, EventArgs e)
{
this.sldProgress.ValueChanged -=
new RoutedPropertyChangedEventHandler<double>(sldProgress_ValueChanged);
this.sldProgress.Value = this.mdeMusic.Position.TotalSeconds / 10.0;
this.sldProgress.ValueChanged +=
new RoutedPropertyChangedEventHandler<double>(sldProgress_ValueChanged);
try
{
this.txtMusicName.Text = "正在播放:" + openFile.SafeFileName;
//显示播放时间/总时间
//this.txtMessage.Text = this.mdeMusic.Position.Hours.ToString() + ":" + this.mdeMusic.Position.Minutes.ToString() + ":" + this.mdeMusic.Position.Seconds.ToString() + "/" + this.mdeMusic.NaturalDuration.TimeSpan.Hours.ToString() + ":" + this.mdeMusic.NaturalDuration.TimeSpan.Minutes.ToString() + ":" + this.mdeMusic.NaturalDuration.TimeSpan.Seconds.ToString();
}
catch
{
MessageBox.Show("出错了:" + e);
}
}
}
}