WPF中对于多媒体的支持非常完整,一般都是通过MediaElement来实现的。
http://msdn.microsoft.com/zh-cn/library/ms748248.aspx
一个关键问题就是:MediaElement 的 LoadedBehavior 属性必须设置为 Manual 才能以交互方式停止、暂停和播放媒体。
我自己做的一个小例子
下面可以看看代码基本上还是比较简单的
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="544" Width="811">
<Grid>
<MediaElement Margin="16,23,12,39" Name="mediaElement1" LoadedBehavior="Manual" Source="ITU.WMV" Volume="{Binding ElementName=slider1, Path=Value}"/>
<Button Height="23" HorizontalAlignment="Left" Margin="16,0,0,10" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click">播放</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="97,0,0,10" Name="button2" VerticalAlignment="Bottom" Width="75" Click="button2_Click">暂停</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="182,0,0,10" Name="button3" VerticalAlignment="Bottom" Width="75" Click="button3_Click">音量+</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="263,0,0,10" Name="button4" VerticalAlignment="Bottom" Width="75" Click="button4_Click">音量-</Button>
<Button Height="23" Margin="347,0,367,10" Name="button5" VerticalAlignment="Bottom" Click="button5_Click">快进</Button>
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,286,10" Name="button6" VerticalAlignment="Bottom" Width="75" Click="button6_Click">快退</Button>
<Slider Height="22" HorizontalAlignment="Right" Margin="0,0,12,11" Name="slider1" VerticalAlignment="Bottom" Width="268" />
</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;
namespace WpfApplication2
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
/// <summary>
/// 播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Pause();
}
/// <summary>
/// 增加音量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, RoutedEventArgs e)
{
//mediaElement1.Volume++;
slider1.Value++;
}
/// <summary>
/// 降低音量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, RoutedEventArgs e)
{
//mediaElement1.Volume--;
slider1.Value--;
}
/// <summary>
/// 快进
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, RoutedEventArgs e)
{
mediaElement1.SpeedRatio++;
}
/// <summary>
/// 快退
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, RoutedEventArgs e)
{
mediaElement1.SpeedRatio--;
}
}
}