在本文中,我们将介绍WPF的多媒体功能,包括:
- 如何使用MediaElement和MediaPlayer类来呈现音频或视频内容。
- 如何使用MediaTimeline和Clock类来*制媒控**体的*放播**模式和速度。
- 如何使用SoundPlayerAction类来*放播**声音文件。
- 如何使用VideoDrawing和DrawingContext类来绘制视频内容。
使用MediaElement和MediaPlayer类
MediaElement和MediaPlayer类是用于呈现音频或视频内容的类。这些类可以通过交互或时钟来控制。这些类可以使用Microsoft Windows Media Player 10控件进行媒体*放播**。
MediaElement是一个UIElement,它支持布局,并且可以作为许多控件的内容。它也可以在XAML和代码中使用。MediaPlayer则是为Drawing对象设计的,并且缺乏布局支持。使用MediaPlayer加载的媒体只能通过VideoDrawing或直接与DrawingContext交互来呈现。
MediaElement和MediaPlayer类都有两种媒体模式,分别是独立模式和时钟模式。媒体模式由Clock属性决定。当Clock为null时,媒体对象处于独立模式。当Clock非null时,媒体对象处于时钟模式。默认情况下,媒体对象处于独立模式。
独立模式
在独立模式下,媒体内容驱动媒体*放播**。独立模式具有以下特点:
- 媒体的Uri可以直接指定。
- 媒体*放播**可以直接控制。
- 媒体的Position和SpeedRatio属性可以修改。
媒体可以通过设置MediaElement对象的Source属性或调用MediaPlayer对象的Open方法来加载。要控制独立模式下的媒体*放播**,可以使用媒体对象的控制方法。可用的控制方法有Play, Pause, Close, 和Stop。对于MediaElement,只有当LoadedBehavior设置为Manual时,才能使用这些方法进行交互控制。这些方法在时钟模式下不可用。
下面是一个使用MediaElement在独立模式下*放播**视频文件的示例:
<Window x:Class="WPFTextToSpeech.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<MediaElement Name="media" Source="sample.mp4" LoadedBehavior="Manual"/>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
<Button Content="Play" Click="Play_Click"/>
<Button Content="Pause" Click="Pause_Click"/>
<Button Content="Stop" Click="Stop_Click"/>
</StackPanel>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
namespace WPFTextToSpeech
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Play_Click(object sender, RoutedEventArgs e)
{
media.Play();
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
media.Pause();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
media.Stop();
}
}
}
时钟模式
在时钟模式下,一个MediaTimeline驱动媒体*放播**。时钟模式具有以下特点:
- 媒体的Uri是通过一个MediaTimeline间接设置的。
- 媒体的*放播**速度和持续时间是由一个Clock控制的。
- 媒体的Position和SpeedRatio属性不能修改。
媒体可以通过创建一个MediaTimeline对象,并将其添加到一个Storyboard中来加载。要控制时钟模式下的媒体*放播**,可以使用Storyboard对象的控制方法。可用的控制方法有Begin, Pause, Resume, Stop, Seek, SeekAlignedToLastTick, SkipToFill 和SetSpeedRatio。
下面是一个使用MediaElement在时钟模式下*放播**视频文件的示例:
<Window x:Class="WPFTextToSpeech.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Storyboard x:Key="mediaStoryboard">
<MediaTimeline Source="sample.mp4" Storyboard.TargetName="media"/>
</Storyboard>
</Window.Resources>
<Grid>
<MediaElement Name="media"/>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
<Button Content="Play" Click="Play_Click"/>
<Button Content="Pause" Click="Pause_Click"/>
<Button Content="Stop" Click="Stop_Click"/>
</StackPanel>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace WPFTextToSpeech
{
public partial class MainWindow : Window
{
private Storyboard mediaStoryboard;
public MainWindow()
{
InitializeComponent();
mediaStoryboard = (Storyboard)FindResource("mediaStoryboard");
}
private void Play_Click(object sender, RoutedEventArgs e)
{
mediaStoryboard.Begin();
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
mediaStoryboard.Pause();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
mediaStoryboard.Stop();
}
}
}
使用SoundPlayerAction类
SoundPlayerAction类是用于*放播**声音文件的类。它可以通过设置Source属性来指定声音文件的Uri,或者通过设置SoundPlayer属性来指定一个System.Media.SoundPlayer对象。
SoundPlayerAction类可以与Trigger类一起使用,以便在触发某些事件时*放播**声音。例如,当用户单击一个按钮时,或者当一个窗口加载时,可以*放播**声音。
下面是一个使用SoundPlayerAction在窗口加载时*放播**声音文件的示例:
<Window x:Class="WPFTextToSpeech.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<SoundPlayerAction Source="sample.wav"/>
</EventTrigger>
</Window.Triggers>
<Grid>
</Grid>
</Window>
使用VideoDrawing和DrawingContext类
VideoDrawing和DrawingContext类是用于绘制视频内容的类。VideoDrawing类表示一种Drawing对象,它可以将视频内容绘制到矩形区域中。VideoDrawing类有两个属性:Rect和Player。Rect属性指定视频内容绘制的矩形区域,Player属性指定一个MediaPlayer对象,它提供视频内容的源。
DrawingContext类表示一种绘图命令的集合,它可以用于描述可视对象或绘图对象。DrawingContext类提供了一系列的Draw方法,用于绘制不同类型的图形元素,如文本、几何图形、图像等。其中,DrawVideo方法可以用于绘制VideoDrawing对象。
要使用VideoDrawing和DrawingContext类,需要创建一个自定义控件,并重写其OnRender方法。OnRender方法是在控件需要重新绘制时调用的方法,它提供了一个DrawingContext参数,用于执行绘图命令。
下面是一个使用VideoDrawing和DrawingContext在自定义控件中绘制视频内容的示例:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFTextToSpeech
{
public class VideoControl : Control
{
private MediaPlayer player;
private VideoDrawing videoDrawing;
public VideoControl()
{
player = new MediaPlayer();
videoDrawing = new VideoDrawing();
videoDrawing.Rect = new Rect(0, 0, 300, 200);
videoDrawing.Player = player;
}
public string Source
{
get { return (string)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(string), typeof(VideoControl), new PropertyMetadata(null, OnSourceChanged));
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (VideoControl)d;
var source = (string)e.NewValue;
control.player.Open(new Uri(source));
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawVideo(videoDrawing, videoDrawing.Rect);
}
}
}
<Window x:Class="WPFTextToSpeech.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTextToSpeech"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:VideoControl Source="sample.mp4"/>
</Grid>
</Window>