MSDN 教程短片 WPF 17(简单播放器的制作)
2011-01-24 08:23
567 查看
WPF做一个简单播放器
WPF中有个MediaPlayer类,它位于System.Windows.Media命名空间下。基于WindowsMediaPlayer构建起来的,所以下面做的播放器的前提是电脑上必须安装WindowsMediaPlayer10或更高版本。一、打开vs,新建一个应用程序项目。
我们在窗体拖入一个MediaElement控件
<MediaElementName="mediaElement1"Margin="0,0,0,61"> </MediaElement>
下面我们来触发这个控件的播放。
<Grid.Triggers> <EventTriggerSourceName="mediaElement1"
RoutedEvent="MediaElement.Loaded"> <EventTrigger.Actions> <BeginStoryboardx:Name="sb"> <Storyboard> <MediaTimeline Source="xbox.wmv" Storyboard.TargetName="mediaElement1" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </Grid.Triggers>
这里我们在MediaElement.Loaded加载的时候开始显示。
这里要注意Source这里的xbox.wmv放在bin目录下。也可以用..\..\xbox.wmv来读取你放的位置。
这是可以按F5运行。
下面我们来增加些功能,比如加个暂停,重新开始,拖拉进度。
拖入两个Button按钮和一个Slider。
<ButtonHeight="23"HorizontalAlignment="Left"Margin="68,0,0,7"
Name="btnPause"VerticalAlignment="Bottom"Width="75">Pause</Button> <ButtonHeight="23"Margin="162,0,126,7"Name="btnResume"
VerticalAlignment="Bottom">Resume</Button> <SliderHeight="21"Margin="43,0,55,37"Name="timeSlider"
VerticalAlignment="Bottom"/> 在Grid.Trigger里添加触发它的事件。
<EventTriggerSourceName="btnPause"RoutedEvent="Button.Click"> <EventTrigger.Actions> <PauseStoryboardBeginStoryboardName="sb"/> </EventTrigger.Actions> </EventTrigger> <EventTriggerSourceName="btnResume"RoutedEvent="Button.Click"> <EventTrigger.Actions> <ResumeStoryboardBeginStoryboardName="sb"/> </EventTrigger.Actions> </EventTrigger> 给MediaElement一个MediaOpened事件叫mediaElement1_MediaOpened
和在MediaTimeline里加个事件CurrentTimeInvalidated=
"MediaTimeline_CurrentTimeInvalidated"后台代码是:
privatevoidmediaElement1_MediaOpened(objectsender,RoutedEventArgse) { timeSlider.Maximum=mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds; } privatevoidMediaTimeline_CurrentTimeInvalidated(objectsender,EventArgse) { timeSlider.Value=mediaElement1.Position.TotalMilliseconds; } 这里timeSlider的最大时间点也就是结束点是mediaElement1总的秒数。
timeSlider随着mediaElement1的播放而移动。
效果:
相关文章推荐
- MSDN 教程短片 WPF 16(Path路径)
- MSDN 教程短片 WPF 19(绑定2-绑定资源模板)
- MSDN 教程短片 WPF 22(XPS文件处理)
- 推荐一个学习WPF的好视频教程(MSDN 教學短片)
- MSDN 教程短片 WPF 23(3D动画)
- MSDN 教程短片 WPF 8(WPF样式与资源)
- MSDN 教程短片 WPF 20(绑定3-ObjectDataProvider)
- MSDN 教程短片 WPF 21(绑定4-TreeView,Menu)
- MSDN 教程短片 WPF 15(DoubleAnimation)
- ffmpeg+sdl教程----编写一个简单的播放器5(同步视频到音频)
- WPF动画制作简单的按钮动画
- 【COCOS CREATOR 系列教程之四】基于0.7.1先简单制作一个PAGEVIEW
- 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四十一)制作精美的Mini地图①
- 用Python制作简单的钢琴程序的教程
- 大学生简单网页个人主页制作大学生网页作业html&css视频教程
- mac 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
- 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
- (译)如何使用GameCenter制作一个简单的多人游戏教程:第一部分
- ffmpeg+sdl教程——编写一个简单的播放器1