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


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

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

    一个关键问题就是:MediaElementLoadedBehavior 属性必须设置为 Manual 才能以交互方式停止、暂停和播放媒体。

    我自己做的一个小例子

    image

    下面可以看看代码基本上还是比较简单的

    <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--;
            }
        }
    }

  • 相关阅读:
    eth私有节点搭建
    angularjs 文件类型格式校验
    Windows Server 2019, 2016 or 2012 安装telnet
    Oracle导致Redo日志暴增的SQL语句排查
    log4j waf 绕过技巧
    jtemplate使用笔记
    NPOI2.0
    关于java同一个类互相调用,spring事务失效问题
    java 将redis的数据批量取出来的时候遇到json格式转换异常
    超级好用的文档转换工具
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1451028.html
Copyright © 2020-2023  润新知