2021-08-26 WPF控件专题 MediaElement 控件详解
Posted 微软MVP Eleven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-08-26 WPF控件专题 MediaElement 控件详解相关的知识,希望对你有一定的参考价值。
一:控件介绍
1.MediaElement 控件介绍
媒体播放控件:音频或视频文件,
属性:
LoadedBehavior 加载行为 (Manual 手动控制 Play Close(关闭并释放) Pause Stop关闭但不释放)
Position 通过媒体的播放时间获取或设置进度的当前位置。
CanPause(get) IsBuffering 是否缓冲媒体 HasAudio/HasVideo 是否有音频/视频
SpeedRatio 媒体的速率 UnloadedBehavior 卸载行为
Clock 与相关联的时钟 Balance 扬声器的音量比 Volume 媒体的音量
StretchDirection 拉伸值,该值指定该元素的方向 Source 媒体源 IsMuted 是否已静音
2.具体案例
<Window x:Class="WpfAppTest.MediaElementWindow"
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:WpfAppTest"
mc:Ignorable="d"
Title="MediaElementWindow" Height="450" Width="800">
<Grid>
<Border BorderThickness="2" BorderBrush="YellowGreen" HorizontalAlignment="Left" Height="275" Margin="43,26,0,0" VerticalAlignment="Top" Width="583">
<MediaElement Source="medias/tongyao1.mp4" IsMuted="False" Volume="0.3" SpeedRatio="1" UnloadedBehavior="Stop" Balance="-0.6" StretchDirection="Both" Stretch="Fill" />
</Border>
</Grid>
</Window>
二:控件案例
<Window x:Class="WpfAppTest.MediaElementWindow2"
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:WpfAppTest"
mc:Ignorable="d"
Title="MediaElementWindow2" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="White"/>
<GradientStop Offset="0.444" Color="#FF53897A"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontStyle" Value="Oblique"/>
<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>
<StackPanel>
<Border Background="Black" BorderThickness="3" HorizontalAlignment="Center" Height="300" VerticalAlignment="Top" Width="500">
<Border.Effect>
<DropShadowEffect Color="#FFE4CC8D"/>
</Border.Effect>
<Border.BorderBrush>
<LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
<GradientStop Offset="0" Color="White"/>
<GradientStop Offset="0.5" Color="Gold"/>
</LinearGradientBrush>
</Border.BorderBrush>
<MediaElement Name="me" LoadedBehavior="Manual" SpeedRatio="1" Stretch="Fill" UnloadedBehavior="Stop" ScrubbingEnabled="True" Volume="{Binding ElementName=volumeSlider,Path=Value}" Balance="0.6" MouseLeftButtonDown="Me_MouseLeftButtonDown" MediaOpened="Me_MediaOpened" MediaEnded="Me_MediaEnded" />
</Border>
<StackPanel Margin="0,10,0,0" Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="播放进度" Foreground="Gold" Margin="5"/>
<Slider x:Name="posSlider" Minimum="0" Width="190" Value="{Binding ElementName=me,Path=Position}" IsMoveToPointEnabled="True" PreviewMouseLeftButtonUp="PosSlider_PreviewMouseLeftButtonUp" />
<TextBlock Text="音量" Foreground="Gold" Margin="20,0,0,0"/>
<Slider x:Name="volumeSlider" Margin="10,0" Minimum="0" Maximum="1" Value="0.5" Width="167" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10">
<Button x:Name="openBtn" Content="打开" Style="{StaticResource btnStyle}" Click="OpenBtn_Click"
/>
<Button x:Name="playBtn" Content="播放" Style="{StaticResource btnStyle}" Click="PlayBtn_Click"
/>
<Button x:Name="stopBtn" Content="停止" Style="{StaticResource btnStyle}" Click="StopBtn_Click"
/>
<Button x:Name="backBtn" Content="快退" Style="{StaticResource btnStyle}" Click="BackBtn_Click"
/>
<Button x:Name="forwardBtn" Content="快进" Style="{StaticResource btnStyle}" Click="ForwardBtn_Click"
/>
<Label Name="lblTime" Width="100" Height="30" Content="00:04"></Label>
</StackPanel>
</StackPanel>
</Grid>
</Window>
System.Timers.Timer timer = null;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += Timer_Elapsed;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//子线程执行
this.Dispatcher.Invoke(new Action(() =>
{
//主线程
posSlider.Value = me.Position.TotalSeconds;
SetTime();
}
));
}
/// <summary>
/// 设置时间文本框的值
/// </summary>
private void SetTime()
{
lblTime.Content = string.Format("{0:00}:{1:00}:{2:00}", me.Position.Hours, me.Position.Minutes, me.Position.Seconds);
}
//选择播放文件
private void OpenBtn_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = @"视频文件(*.avi格式)|*.avi|视频文件(*.wav格式)|*.wav|视频文件(*.wmv格式)|*.wmv|视频文件(*.mp4格式)|*.mp4|All Files|*.*";
if (ofd.ShowDialog() == false)
{
return;
}
string filePath = "";
filePath = ofd.FileName;
if (filePath == "")
return;
//设置媒体源
me.Source = new Uri(filePath, UriKind.Absolute);
playBtn.IsEnabled = true;
me.Play();
timer.Start();
playBtn.Content = "暂停";
}
//播放或暂停
private void PlayBtn_Click(object sender, RoutedEventArgs e)
{
SetPlayer(true);
PlayPause();
}
/// <summary>
/// 播放或暂停
/// </summary>
private void PlayPause()
{
if (playBtn.Content.ToString() == "播放")
{
me.Play();
timer.Start();
playBtn.Content = "暂停";
me.ToolTip = "单击暂停";
}
else
{
me.Pause();
timer.Stop();
playBtn.Content = "播放";
me.ToolTip = "单击播放";
SetTime();
}
}
/// <summary>
/// 设置操作按钮的可用状态
/// </summary>
/// <param name="bl"></param>
private void SetPlayer(bool bl)
{
stopBtn.IsEnabled = bl;
playBtn.IsEnabled = bl;
backBtn.IsEnabled = bl;
forwardBtn.IsEnabled = bl;
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopBtn_Click(object sender, RoutedEventArgs e)
{
me.Stop();
posSlider.Value = 0;
lblTime.Content = "0:0:0";
playBtn.Content = "播放";
timer.Stop();
}
/// <summary>
/// 快进 当前播放位置前进10s
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ForwardBtn_Click(object sender, RoutedEventArgs e)
{
me.Pause();
timer.Stop();
me.Position = me.Position + TimeSpan.FromSeconds(10);
SetTime();
me.Play();
timer.Start();
}
/// <summary>
/// 快退
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BackBtn_Click(object sender, RoutedEventArgs e)
{
me.Pause();
timer.Stop();
me.Position = me.Position - TimeSpan.FromSeconds(10);
SetTime();
me.Play();
timer.Start();
}
private void Me_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
PlayPause();
}
/// <summary>
/// 媒体加载完毕后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Me_MediaOpened(object sender, RoutedEventArgs e)
{
posSlider.Maximum = me.NaturalDuration.TimeSpan.TotalSeconds;
lblTime.Content = "0:0:0";
SetPlayer(true);
}
/// <summary>
///拖动播放进度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PosSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
me.Pause();
timer.Stop();
int val = (int)posSlider.Value;
me.Position = new TimeSpan(0, 0, 0, val);
SetTime();
me.Play();
timer.Start();
}
private void Me_MediaEnded(object sender, RoutedEventArgs e)
{
MessageBox.Show("播放结束");
timer.Stop();
}
以上是关于2021-08-26 WPF控件专题 MediaElement 控件详解的主要内容,如果未能解决你的问题,请参考以下文章
2021-08-19 WPF控件专题 TabControl 控件详解
2021-08-17 WPF控件专题 Groupbox 控件详解