码迷,mamicode.com
首页 > 其他好文 > 详细

WPF使用MediaElement方法介绍

时间:2014-06-21 13:14:18      阅读:690      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

搬运自:http://wurang.me/2014/04/29/WPF-MediaElement.html

 

在WPF或Silverlight中使用MediaElement控件可以方便的制作一个视频音频播放器。

首先制作一个UserControl:

bubuko.com,布布扣

代码如下:

  <DockPanel Height="387">
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
            <Button Height="23" HorizontalAlignment="Left" Margin="16,0,0,10" x:Name="btnPlay" VerticalAlignment="Bottom" Width="75" Click="btnPlay_Click">播放</Button>
            <Button Height="23" HorizontalAlignment="Left" Margin="16,0,0,10" x:Name="btnPause" VerticalAlignment="Bottom" Width="75" Click="btnPause_Click">暂停</Button>
            <Label Content="音量:"/>
            <Slider x:Name="sldVolumn" Value="{Binding Volume, ElementName=mePlayer, Mode=TwoWay, UpdateSourceTrigger=Default}" Maximum="1" LargeChange="0.1" SmallChange="0.01" Width="124" />
            <Label Content="进度:"/>
            <Slider x:Name="sldProgress" Minimum="0" Value="{Binding ElementName=mePlayer, Path=Position, Mode=TwoWay, Converter={StaticResource PrgConverter}}" LargeChange="10" Width="192" />
        </StackPanel>
        <MediaElement DockPanel.Dock="Top"   Margin="16,23,12,39" x:Name="mePlayer" LoadedBehavior="Manual" MediaOpened="mePlayer_MediaOpened"  />
       </DockPanel>

 

这里音量控制使用Slider并Binding到MediaElement上,同时要设置最大值Maximum为1

接下来需要制作一个进度条,通过拖动进度条可以控制视频播放进度。

进度条也是用Slider,我们需要把它的Value和MediaElement的Position属性关联起来,所以需要一个Converter。

 public class ProgressConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((TimeSpan)value).TotalSeconds;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return TimeSpan.FromSeconds((double)value);
        }
    }

 

在这个Converter中,我们把Slider和播放视频的总时间做对应,所以还需要将视频的总时间赋值给Slider的Maximum属性。由于每个视频的时间是不同的,所以必须要通过动态的方法赋值,在MediaElement的MediaOpened事件中可以完成这一操作:

 private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            mePlayer.Play();
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            mePlayer.Pause();
        }

        private void mePlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            sldProgress.Maximum = mePlayer.NaturalDuration.TimeSpan.TotalSeconds;
        }

 

需要注意的是MSDN中有对NaturalDuration的备注:在引发 MediaOpened 事件之前,NaturalDuration 是不正确的。

所以mePlayer.NaturalDuration.TimeSpan.TotalSeconds不能在构造函数或者其他在MediaOpened事件前的方法中调用。

WPF使用MediaElement方法介绍,布布扣,bubuko.com

WPF使用MediaElement方法介绍

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/wurang/p/3794966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!