当我们在实际开发中,要想在程序中添加音乐的话,可以使用一个叫做WPF MediaElement的类。那么具体的使用方法又是怎样的呢?#t#
一个朋友问了我个问题,说怎么在wpf程序中增加音乐.搜索了下msdn看到了个WPF MediaElement.
把用法总结下贴出来.
WPF MediaElement是一个播放audio和video的类.
前台加入以下代码:
- < StackPanel Background="Black">
- < MediaElement Source="numbers.
wmv" Name="myMediaElement" - Width="450" Height="250"
LoadedBehavior="Manual"
UnloadedBehavior="Stop" Stretch="Fill" - MediaOpened="Element_MediaOpened"
MediaEnded="Element_MediaEnded"/> - < StackPanel HorizontalAlignment=
"Center" Width="450"
Orientation="Horizontal"> - < !-- 播放按钮. -->
- < Image Source="3D_menu_07.png"
MouseDown="OnMouseDownPlayMedia"
Margin="5" /> - < !-- 暂停按钮. -->
- < Image Source="3D_menu_09_2.png"
MouseDown="OnMouseDownPauseMedia"
Margin="5" /> - < !-- 停止按钮. -->
- < Image Source="list4_01.png"
MouseDown="OnMouseDownStopMedia"
Margin="5" /> - < !-- 音量设置. -->
- < TextBlock Foreground="White"
VerticalAlignment="Center"
Margin="5" >Volume< /TextBlock> - < Slider Name="volumeSlider"
VerticalAlignment="Center"
ValueChanged="ChangeMediaVolume" - Minimum="0" Maximum="1" Value="0.5"
Width="70"/> - < !-- 播放速度设置. -->
- < TextBlock Foreground="White"
Margin="5" VerticalAlignment=
"Center">Speed< /TextBlock> - < Slider Name="speedRatioSlider"
VerticalAlignment="Center"
ValueChanged="ChangeMediaSpeedRatio" - Value="1" Width="70" />
- < !-- 播放进度. -->
- < TextBlock Foreground="White"
Margin="5" VerticalAlignment=
"Center">Seek To< /TextBlock> - < Slider Name="timelineSlider"
Margin="5" ValueChanged=
"SeekToMediaPosition" Width="70"/> - < /StackPanel>
- < /StackPanel>
后台加入以下WPF MediaElement代码:
- void OnMouseDownPlayMedia
(object sender, MouseButton
EventArgs args)- ...{
- myMediaElement.Play();
//播放加载好的视频文件.- InitializePropertyValues();
- }
- void OnMouseDownPauseMedia
(object sender, MouseButton
EventArgs args)- ...{
- myMediaElement.Pause();//暂停播放
- }
- // Stop the media.
- void OnMouseDownStopMedia
(object sender, MouseButton
EventArgs args)- ...{
- myMediaElement.Stop();
//停止播放,再次播放会从头开始- }
- private void ChangeMediaVolume
(object sender, RoutedProperty
ChangedEventArgs< double> args)- ...{
- myMediaElement.Volume =
(double)volumeSlider.Value;//更改音量- }
- private void ChangeMediaSpeed
Ratio(object sender,
RoutedPropertyChangedEventArgs
< double> args)- ...{
- myMediaElement.SpeedRatio =
(double)speedRatioSlider.Value;
//更改播放速度- }
- //播放开始调用的事件
- private void Element_MediaOpened
(object sender, EventArgs e)- ...{
- //设置播放进度条的***值
- timelineSlider.Maximum =
myMediaElement.NaturalDuration.
TimeSpan.TotalMilliseconds;- }
- //播放结束调用事件
- private void Element_MediaEnded
(object sender, EventArgs e)- ...{
- myMediaElement.Stop();
- }
- //播放进度
- private void SeekToMediaPosition
(object sender, RoutedProperty
ChangedEventArgs< double> args)- ...{
- int SliderValue = (int)timeline
Slider.Value;- TimeSpan ts = new TimeSpan
(0, 0, 0, 0, SliderValue);- myMediaElement.Position = ts;
//这里跳转到播放的哪个地方- }
- //点击开始时设置播放的音量和播放速度
- void InitializePropertyValues()
- ...{
- myMediaElement.Volume =
(double)volumeSlider.Value;- myMediaElement.SpeedRatio =
(double)speedRatioSlider.Value;- }
以上就是WPF MediaElement的相关介绍。