该篇文章是我于2009年6月10日通过自己编写的工具,批量从位于在博客园的博客站点(http://chenxizhang.cnblogs.com)同步而来。文章中的图片地址仍然是链接到博客园的。特此说明! 陈希章原文地址:http://www.cnblogs.com/chenxizhang/archive/2009/05/06/1451028.html原文标题:控制 MediaElement(播放、暂停、停止、音量和速度) 原文发表:2009/5/6 13:54:00 |
WPF中对于多媒体的支持非常完整,一般都是通过MediaElement来实现的。
http://msdn.microsoft.com/zh-cn/library/ms748248.aspx
一个关键问题就是:MediaElement 的 LoadedBehavior 属性必须设置为 Manual 才能以交互方式停止、暂停和播放媒体。
我自己做的一个小例子
下面可以看看代码基本上还是比较简单的
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="544" Width="811">
LoadedBehavior="Manual" Source="ITU.WMV" Volume
="{Binding ElementName=slider1, Path=Value}"/>
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
{
///
/// Window1.xaml 的交互逻辑
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
///
/// 播放
///
///
///
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
///
/// 暂停
///
///
///
private void button2_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Pause();
}
///
/// 增加音量
///
///
///
private void button3_Click(object sender, RoutedEventArgs e)
{
//mediaElement1.Volume++;
slider1.Value++;
}
///
/// 降低音量
///
///
///
private void button4_Click(object sender, RoutedEventArgs e)
{
//mediaElement1.Volume--;
slider1.Value--;
}
///
/// 快进
///
///
///
private void button5_Click(object sender, RoutedEventArgs e)
{
mediaElement1.SpeedRatio++;
}
///
/// 快退
///
///
///
private void button6_Click(object sender, RoutedEventArgs e)
{
mediaElement1.SpeedRatio--;
}
}
}
作者:陈希章 出处:http://blog.youkuaiyun.com/chen_xizhang 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |