基于WPF实现一个简单的音频播放动画控件

目录
  • 1.实现代码
  • 2.效果预览

1.实现代码

一、创建AnimationAudio.xaml代码如下

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:WPFDevelopers.Controls"
                    xmlns:helpers="clr-namespace:WPFDevelopers.Helpers">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
        <ResourceDictionary Source="Basic/Animations.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    

    <Style TargetType="{x:Type controls:AnimationAudio}" BasedOn="{StaticResource ControlBasicStyle}">
        <Setter Property="Width" Value="80"/>
        <Setter Property="Height" Value="35"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Foreground" Value="{DynamicResource WhiteSolidColorBrush}"/>
        <Setter Property="Background" Value="{DynamicResource PrimaryNormalSolidColorBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:AnimationAudio}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="PlayStoryboard" RepeatBehavior="Forever">
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PathAudioTwo" Storyboard.TargetProperty="(Path.Visibility)">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Hidden}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PathAudioThree" Storyboard.TargetProperty="(Path.Visibility)">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Hidden}" />
                            </ObjectAnimationUsingKeyFrames>

                            <ObjectAnimationUsingKeyFrames BeginTime="0:0:.3" Duration="0:0:.4" Storyboard.TargetName="PathAudioTwo"
                                       Storyboard.TargetProperty="(Path.Visibility)">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames BeginTime="0:0:.7" Duration="0:0:.4" Storyboard.TargetName="PathAudioThree"
                                       Storyboard.TargetProperty="(Path.Visibility)">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Border x:Name="PART_Border" Background="{TemplateBinding Background}" 
                            CornerRadius="{TemplateBinding helpers:ControlsHelper.CornerRadius}"
                            SnapsToDevicePixels="True" UseLayoutRounding="True">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Width="20" Height="30" HorizontalAlignment="Left" 
                                        Orientation="Horizontal" Margin="10,0"
                                        RenderTransformOrigin=".5,.5"
                                        x:Name="PART_StackPanel">
                                
                                <Path Data="{StaticResource PathAudioOne}" Width="4" Height="6" 
                                      Stretch="Fill" Fill="{TemplateBinding Foreground}"/>
                                <Path x:Name="PathAudioTwo" Data="{StaticResource PathAudioTwo}" Width="6" StrokeThickness="1.5" 
                                      Stroke="Transparent" 
                                      Margin="0,7" Stretch="Fill" Fill="{TemplateBinding Foreground}"/>
                                <Path x:Name="PathAudioThree" Data="{StaticResource PathAudioThree}" Width="8" Margin="-3,4" Stretch="Fill" 
                                      Fill="{TemplateBinding Foreground}" StrokeThickness="2" Stroke="Transparent"/>
                            </StackPanel>
                            <TextBlock VerticalAlignment="Center" 
                                       Foreground="{TemplateBinding Foreground}"
                                       FontSize="{DynamicResource TitleFontSize}"
                                       Grid.Column="1"
                                       x:Name="PART_TextBlock">
                                <Run x:Name="PART_RunTimeLength"></Run>
                            </TextBlock>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPlay" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="PlayBeginStoryboard" Storyboard="{StaticResource PlayStoryboard}"/>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="PlayBeginStoryboard"/>
                            </Trigger.ExitActions>
                        </Trigger>
                        <Trigger Property="IsRight" Value="True">
                            <Setter Property="Grid.Column" TargetName="PART_TextBlock" Value="0"/>
                            <Setter Property="HorizontalAlignment" TargetName="PART_TextBlock" Value="Right"/>
                            <Setter Property="Grid.Column" TargetName="PART_StackPanel" Value="1"/>
                            <Setter Property="HorizontalAlignment" TargetName="PART_StackPanel" Value="Right"/>
                            <Setter Property="RenderTransform" TargetName="PART_StackPanel">
                                <Setter.Value>
                                    <TransformGroup>
                                        <RotateTransform Angle="180"/>
                                    </TransformGroup>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

二、创建AnimationAudioe.cs代码如下

using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Interop;
using WPFDevelopers.Helpers;

namespace WPFDevelopers.Controls
{
    [TemplatePart(Name = RunTemplateName, Type = typeof(Run))]
    public partial class AnimationAudio : Control
    {
        const string RunTemplateName = "PART_RunTimeLength";

        private Run _run;
        private TimeSpan _timeSpan;
        private IntPtr _handle;
        private AudioWindow _win = null;

        static string[] mediaExtensions = { ".MP3", ".WAV" };
        /// <summary>
        /// 音频路径
        /// </summary>
        public string AudioPath
        {
            get { return (string)GetValue(AudioPathProperty); }
            set { SetValue(AudioPathProperty, value); }
        }
        public static readonly DependencyProperty AudioPathProperty =
            DependencyProperty.Register("AudioPath", typeof(string), typeof(AnimationAudio), new PropertyMetadata(string.Empty));

        /// <summary>
        /// 是否右侧
        /// </summary>
        public bool IsRight
        {
            get { return (bool)GetValue(IsRightProperty); }
            set { SetValue(IsRightProperty, value); }
        }
        public static readonly DependencyProperty IsRightProperty =
            DependencyProperty.Register("IsRight", typeof(bool), typeof(AnimationAudio), new PropertyMetadata(false));

        public bool IsPlay
        {
            get { return (bool)GetValue(IsPlayProperty); }
            set { SetValue(IsPlayProperty, value); }
        }

        public static readonly DependencyProperty IsPlayProperty =
            DependencyProperty.Register("IsPlay", typeof(bool), typeof(AnimationAudio), new PropertyMetadata(false, new PropertyChangedCallback(OnIsPlayChanged)));

        private static void OnIsPlayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool newValue = (bool)e.NewValue;
            var animationAudio = d as AnimationAudio;
            if(newValue != (bool)e.OldValue)
            {
                if (newValue)
                {
                    animationAudio.Play();
                }
                else
                {
                    AudioPlayer.Stop();
                }
            }
        }

        static AnimationAudio()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AnimationAudio), new FrameworkPropertyMetadata(typeof(AnimationAudio)));
        }

       
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _run = GetTemplateChild(RunTemplateName) as Run;
            if (string.IsNullOrWhiteSpace(AudioPath)) return;
            if (!File.Exists(AudioPath)) return;
            if (!mediaExtensions.Contains(Path.GetExtension(AudioPath), StringComparer.OrdinalIgnoreCase)) return;
            _timeSpan = AudioPlayer.GetSoundLength(AudioPath);
            if (_timeSpan == TimeSpan.Zero) return;
            _run.Text = $"{_timeSpan.Seconds.ToString()}\"";
            Width = 80;
            if (_timeSpan.Seconds > 5)
            {
                Width += _timeSpan.Seconds;
            }
        }

        private void Play()
        {
            if(_win != null)
            {
                _win.Close();
                _win = null;
            }
            _win = new AudioWindow
            {
                Width = 0,
                Height = 0,
                Left = Int32.MinValue,
                Top = Int32.MinValue,
                WindowStyle = WindowStyle.None,
                ShowInTaskbar = false,
                ShowActivated = false,
            };
            _win.Show();
            _win.StopDelegateEvent += _win_StopDelegateEvent;
            _handle = new WindowInteropHelper(_win).Handle;
            AudioPlayer.PlaySong(AudioPath, _handle);
        }

       
        private void _win_StopDelegateEvent()
        {
            IsPlay = false;
            _win.Close();
            _win = null;
        }
    }
}

三、新建AudioWindow.cs代码如下

using System;
using System.Windows;
using System.Windows.Interop;

namespace WPFDevelopers.Controls
{
    public class AudioWindow:Window
    {
        const int MM_MCINOTIFY = 0x3B9;
        public delegate void StopDelegate();
        public event StopDelegate StopDelegateEvent;
        
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource != null)
            {
                hwndSource.AddHook(new HwndSourceHook(this.WndProc));
            }
        }
        
        IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case MM_MCINOTIFY:
                    StopDelegateEvent?.Invoke();
                    break;
            }
            return IntPtr.Zero;
        }
    }
}

四、新建AnimationAudioExample.xaml代码如下。

  <UserControl x:Class="WPFDevelopers.Samples.ExampleViews.AnimationAudioExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
             xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">

    <UniformGrid Columns="2" x:Name="MyUniformGrid">
        <StackPanel Orientation="Horizontal">
            <wpfdev:BreathLamp Width="60" Height="60" 
                               LampEffect="Ripple"
                               IsLampStart="true"
                               Margin="10,0">
                <Ellipse Width="50" Height="50">
                    <Ellipse.Fill>
                        <ImageBrush ImageSource="pack://application:,,,/WPFDevelopers.Samples;component/Images/Breathe/0.jpg"/>
                    </Ellipse.Fill>
                </Ellipse>
            </wpfdev:BreathLamp>
            <wpfdev:AnimationAudio x:Name="AnimationAudioLeft" MouseDown="AnimationAudioLeft_MouseDown"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Right">
            <wpfdev:AnimationAudio x:Name="AnimationAudioRight" IsRight ="true" 
                               Background="{DynamicResource SuccessSolidColorBrush}"
                               Foreground="Black"
                               MouseDown="AnimationAudioLeft_MouseDown"/>
            <wpfdev:BreathLamp Width="50" Height="50" 
                               LampEffect="Streamer"
                               Background="LightGray"
                               IsLampStart="True"
                               Margin="10,0">
                <Ellipse Width="43" Height="43">
                    <Ellipse.Fill>
                        <ImageBrush ImageSource="pack://application:,,,/WPFDevelopers.Samples;component/Images/Chat/UserImages/yanjinhua.png"/>
                    </Ellipse.Fill>
                </Ellipse>
            </wpfdev:BreathLamp>
        </StackPanel>
       
    </UniformGrid>
</UserControl>
                 

六、新建AnimationAudioExample.xaml.cs下

using System;
using System.IO;
using System.Windows.Controls;
using WPFDevelopers.Controls;
using WPFDevelopers.Samples.Helpers;

namespace WPFDevelopers.Samples.ExampleViews
{
    /// <summary>
    /// 微信公众号:WPF开发者
    /// </summary>
    public partial class AnimationAudioExample : UserControl
    {
        public AnimationAudioExample()
        {
            InitializeComponent();
            AnimationAudioLeft.AudioPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "Audio", "HelloWPFDevelopes_en.mp3");
            AnimationAudioRight.AudioPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "Audio", "HelloWPFDevelopes_zh.mp3");
        }

        private void AnimationAudioLeft_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var animationAudio = sender as AnimationAudio;
            var animationAudioList = ElementVisualTreeHelper.FindVisualChild<AnimationAudio>(MyUniformGrid);
            if (animationAudioList == null) return;
            if (!animationAudio.IsPlay)
            {
                animationAudioList.ForEach(h =>
                {
                    if (h.IsPlay && h != animationAudio)
                    {
                        h.IsPlay = false;
                    }
                });
                animationAudio.IsPlay = true;
            }
            else
                animationAudio.IsPlay = false;
        }
    }
}

2.效果预览

到此这篇关于基于WPF实现一个简单的音频播放动画控件的文章就介绍到这了,更多相关WPF音频播放动画控件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 关于.NET/C#/WCF/WPF 打造IP网络智能视频监控系统的介绍

    OptimalVision网络视频监控系统 OptimalVision(OV)网络视频监控系统(Video Surveillance System),是一套基于.NET.C#.WCF.WPF等技术构建的IP网络视频监控系统.设计与实现该系统的初衷是希望在家用电脑中部署该系统,连接本地或局域网设备,通过浏览器或手机客户端浏览宝宝实时视频,也就是俗称的"宝宝在线"或"家庭看护". 但由于业余时间总是有限,完成系统中的服务.配置.采集.传输和桌面GUI部分后,继续完成后续

  • C# WPF实现的语音播放自定义控件

    原理很简单,利用Path画一个图,然后用动画进行播放,播放时间由依赖属性输入赋值与控件内部维护的一个计时器进行控制. 控件基本是玩具,无法作为真实项目使用. 因为没有设置播放源,所以编写异步播放源或者实际播放时候要将事件引发,是否播放等属性,事件移到真实播放事件 非专业UI,即使知道怎么画图也是画的不如意,到底是眼睛会了,手不行啊. 主界面xaml <local:VoiceAnimeButton Height="40" Width="200" IconMarg

  • C#中WPF颜色对话框控件的实现

    在 C# WPF开发中颜色对话框控件(ColorDialog)用于对界面中的背景.文字…(拥有颜色属性的所有控件)设置颜色,例如设置标签控件的背景色. 颜色对话框的运行效果如下图所示: 标签背景色设置后如下: xml代码: <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http

  • 解析WPF实现音频文件循环顺序播放的解决方法

    要做基于WPF的音频文件循环顺序播放首先要了解WPF下有哪些类是用于控制音频的. WPF下主要有两个音频控制的类,这里做下比较: 1.SoundPlayer 2.MediaPlayer 派生MediaElement 一.SoundPlayer类   1.基于.NET FRAMEWORK 2.0: 2.可播放WAV音频文件: 3.只能播放一个文件,同时播放多个文件会后一个文件的播放操作会终止前一个播放的文件: 4.不能对音量进行控制:二.MediaPlayer类 1.基于WPF: 2.支持多种音频

  • WPF中在摄像头视频上叠加控件的解决方案

    说道WPF想必有很多朋友跟小编一样不知道wpf是什么,今天小编就给大家简单普及下基本概念. WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的编程模型.语言和框架,真正做到了分离界面设计人员与开发人员的工作:同时它提供了全新的多媒体交互用户图形界面. 一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在win

  • 基于WPF实现一个简单的音频播放动画控件

    目录 1.实现代码 2.效果预览 1.实现代码 一.创建AnimationAudio.xaml代码如下 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    

  • Android基于AdapterViewFlipper实现的图片/文字轮播动画控件

    1. 问题/坑点 1.1 item宽高不生效问题 需要注意的是,AdapterViewFlipper 在布局时,宽高一定要用 match_parent 或者 具体dp值. 如果宽.高中使用了 wrap_content 时,会导致 AdapterViewFlipper 容器的宽高,最终变成第一个item的宽高.即使后续item的宽高超过第一个item,也不会生效,内容显示只会被限定在第一个的宽高范围内. 原理也很好理解,后续item没有绘制出来时, wrap_content 计算出来的结果,就是第

  • 基于JSP实现一个简单计算器的方法

    本文实例讲述了基于JSP实现一个简单计算器的方法.分享给大家供大家参考.具体实现方法如下: index.jsp 复制代码 代码如下: <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  <%  String path = request.getContextPath();  String basePath = request.getSch

  • 原生js基于canvas实现一个简单的前端截图工具代码实例

    这篇文章主要介绍了原生js基于canvas实现一个简单的前端截图工具代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先看效果 代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{

  • 基于JavaScript实现一个简单的Vue

    Object.defineProperty() 实现之前我们得先看一下Object.defineProperty的实现,因为vue主要是通过数据劫持来实现的,通过get.set来完成数据的读取和更新. var obj = {name:'wclimb'} var age = 24 Object.defineProperty(obj,'age',{ enumerable: true, // 可枚举 configurable: false, // 不能再define get () { return a

  • 浅谈基于SpringBoot实现一个简单的权限控制注解

    注解是 JDK 5.0 引入的一种注释机制.注解可以作用在类型(类.接口.枚举等).属性.方法.参数等不同位置,具体的 JDK 版本所支持的注解位置可参考 java.lang.annotation.ElementType .此外还有注解的策略,也就是 RetentionPolicy ,这里不加赘述. 注解可以实现很多功能,其中最主要的就是进行代码标注,所以有时候注解也叫做标注.使用起来也基本顾名思义,就是对代码进行标注,简化部分代码的逻辑. 下面我们就着手实现一个简单的权限控制注解,来对注解有一

  • 基于PyTorch实现一个简单的CNN图像分类器

    pytorch中文网:https://www.pytorchtutorial.com/ pytorch官方文档:https://pytorch.org/docs/stable/index.html 一. 加载数据 Pytorch的数据加载一般是用torch.utils.data.Dataset与torch.utils.data.Dataloader两个类联合进行.我们需要继承Dataset来定义自己的数据集类,然后在训练时用Dataloader加载自定义的数据集类. 1. 继承Dataset类并

  • 基于springboot实现一个简单的aop实例

    简介 AOP(Aspect-Oriented Programming:面向切面编程) aop能将一些繁琐.重复.无关业务的逻辑封装起来,在一个地方进行统一处理,常用于日志记录.事务管理.权限控制等,aop能在不改变原有代码逻辑的基础上对某个方法.某类方法.或者整个类进行无侵入式的加强,有效降低了代码耦合度,并且提高了项目扩展性: ok废话说完,进入正题,如何实现一个aop 要实现aop,首先你要知道你拿aop来干啥,我们今天就以记录日志来说,因为这个最常用,一般对于重要的数据库操作,我们需要记录

  • 基于Java实现一个简单的单词本Android App的实践

    目录 布局设计 代码 AddDanciActivity.java DBOpenHelper.java 本文基于Java实现了一个简单的单词本安卓app,用的是SQLite数据库,包括布局文件.源码及实现图. 布局设计 单词本主界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

  • 基于Python编写一个简单的端口扫描器

    目录 1.需要的库 2.获取一个 host 地址 3.循环所有的端口 4.完整脚本 端口扫描是非常实用的,不止用在信息安全方面,日常的运维也用得到.这方面的工具也不要太多,搞过 CTF 的朋友会告诉你有多少端口扫描工具,那为什么还要用 Python 再自己实现一遍?这个问题就像饭店里的菜已经很好吃了,为什么还要自己烧菜一样,主要还是为了适合自己的口味,添加自己需要的个性功能. 今天我们将用 20 行代码编写一个简单的端口扫描器.让我们开始吧! 1.需要的库 都是标准库,因此内网环境也不影响: i

随机推荐