控制 MediaElement(播放、暂停、停止、音量和速度)


WPF中对于多媒体的支持非常完整,一般都是通过MediaElement来实现的。

http://msdn.microsoft.com/zh-cn/library/ms748248.aspx

一个关键问题就是:MediaElementLoadedBehavior 属性必须设置为 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--;
}
}
} 优质内容筛选与推荐>>
1、hadoop2.4 支持snappy
2、hbase+springboot+redis实现分页
3、hdu 6170
4、Servlet基本_クッキー、URLリライティング
5、电感和感抗


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号