Flex中如何给VideoDisplay添加一个基本的播放/暂停按钮的例子
By Minidxer | July 12, 2008
在前面的Flex的VideoDisplay基本使用方法的例子中我们学习了VideoDisplay的一些基本用法,不过很多时候,我们看到的播放器,开始/暂停控制按钮都是直接在播放画面中的,鼠标移到画面上的时候显示,移出之后隐藏。接下来的例子就演示了Flex中如何给VideoDisplay添加一个基本的播放/暂停按钮。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">
- <mx:Style>
- @font-face {
- src:url("assets/arial.ttf");
- font-family: Arial;
- }
- .timeStyle {
- color: #FFFFFF;
- font-family: Arial;
- font-size: 12;
- }
- .playPauseStyle {
- /* play button skins */
- skin: Embed('assets/control_play.png');
- downSkin: Embed('assets/control_play_blue.png');
- /* pause button skins */
- selectedUpSkin: Embed('assets/control_pause.png');
- selectedOverSkin: Embed('assets/control_pause.png');
- selectedDownSkin: Embed('assets/control_pause_blue.png');
- }
- .stopStyle {
- skin: Embed('assets/control_stop.png');
- downSkin: Embed('assets/control_stop_blue.png');
- }
- .controllerStyle {
- bottom: 5;
- left: 5;
- right: 5;
- paddingBottom: 5;
- paddingLeft: 5;
- paddingRight: 5;
- paddingTop: 5;
- alpha: 0;
- background-color: #000000;
- background-alpha: 0.5;
- }
- </mx:Style>
- <mx:Script>
- <![CDATA[
- import mx.events.VideoEvent;
- private function showControls():void {
- fadeIn.play([controls]);
- }
- private function hideControls():void {
- fadeOut.play([controls]);
- }
- private function videoDisplay_playheadUpdate(evt:VideoEvent):void {
- var pTime:Date = new Date(videoDisplay.playheadTime * 1000 || 100);
- var tTime:Date = new Date(videoDisplay.totalTime * 1000);
- time.text = dateFormatter.format(pTime) + " / " + dateFormatter.format(tTime);
- }
- private function playPauseButton_click(evt:MouseEvent):void {
- if (videoDisplay.playing) {
- videoDisplay.pause();
- } else {
- videoDisplay.play();
- }
- }
- private function stopButton_click(evt:MouseEvent):void {
- videoDisplay.stop();
- }
- ]]>
- </mx:Script>
- <mx:Fade id="fadeIn" alphaFrom="0.0" alphaTo="1.0" />
- <mx:Fade id="fadeOut" alphaFrom="1.0" alphaTo="0.0" />
- <mx:DateFormatter id="dateFormatter" formatString="NN:SS" />
- <mx:Label text="Mouse over the VideoDisplay control below to show control buttons." />
- <mx:Canvas rollOver="showControls()" rollOut="hideControls()">
- <mx:VideoDisplay id="videoDisplay" source="http://blog.minidx.com/ext/caption_video.flv" autoPlay="false" playheadUpdate="videoDisplay_playheadUpdate(event)" />
- <mx:HBox id="controls" styleName="controllerStyle" alpha="0.0">
- <mx:Button id="playPauseButton" styleName="playPauseStyle" toggle="true" selected="{videoDisplay.playing}" click="playPauseButton_click(event)" />
- <mx:Button id="stopButton" styleName="stopStyle" click="stopButton_click(event)" />
- <mx:Spacer width="100%" />
- <mx:Label id="time" styleName="timeStyle" />
- </mx:HBox>
- </mx:Canvas>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer
Topics:
Flex |
Tags: @font face, Embed, skinning, VideoDisplay