




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第基于WPF实现一个简单的音频播放动画控件目录1.实现代码2.效果预览
1.实现代码
一、创建AnimationAudio.xaml代码如下
ResourceDictionary
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/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:
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
xmlns:mc="/markup-compatibility/2006"
xmlns:d="/expression/blend/2008"
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
xmlns:wpfdev="/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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 超市购物赔偿协议书
- 劳动合同带保密协议书
- 锻件产品开发协议书
- 闲置电缆出让协议书
- 营运班车合伙协议书
- 解雇保姆合同协议书
- 阳台封窗安全协议书
- 项目合作摄影协议书
- 酒席用品转让协议书
- 慢性子裁缝的课件
- 乐山市市级事业单位选调工作人员考试真题2024
- DZ∕T 0211-2020 矿产地质勘查规范 重晶石、毒重石、萤石、硼(正式版)
- 新时代中小学教师职业行为十项准则考核试题及答案
- 微纳米定位技术v3课件
- 初中七年级数学下学期5月月考试卷
- 汽机发电量计算
- GB∕T 1457-2022 夹层结构滚筒剥离强度试验方法
- 康复治疗技术(康复养老服务)专业群建设方案
- 第五章结型场效应晶体管
- 丽声北极星自然拼读绘本第一级Uncle Vic‘s Wagon 课件
- 2019幼儿园家委会PPT
评论
0/150
提交评论