| 1 | <?xml version="1.0" encoding="utf-8"?> |
|---|
| 2 | <widget:AbstractWidgetComponent |
|---|
| 3 | xmlns:korsakow="org.korsakow.player.widget" |
|---|
| 4 | xmlns:fx="http://ns.adobe.com/mxml/2009" |
|---|
| 5 | xmlns:s="library://ns.adobe.com/flex/spark" |
|---|
| 6 | xmlns:mx="library://ns.adobe.com/flex/halo" |
|---|
| 7 | xmlns:widget="org.korsakow.player.widget.*" |
|---|
| 8 | |
|---|
| 9 | xmlns:flexcomponents="org.korsakow.flexcomponents.*"> |
|---|
| 10 | <fx:Style> |
|---|
| 11 | @namespace korsakow "org.korsakow.player.widget"; |
|---|
| 12 | @namespace s "library://ns.adobe.com/flex/spark"; |
|---|
| 13 | @namespace mx "library://ns.adobe.com/flex/halo"; |
|---|
| 14 | @namespace widget "org.korsakow.player.widget.*"; |
|---|
| 15 | |
|---|
| 16 | #playTimeLabel, |
|---|
| 17 | #totalTimeLabel |
|---|
| 18 | { |
|---|
| 19 | font-family: monospace; |
|---|
| 20 | } |
|---|
| 21 | </fx:Style> |
|---|
| 22 | <flexcomponents:LayoutCanvas |
|---|
| 23 | id="canvas" |
|---|
| 24 | width="100%" |
|---|
| 25 | minWidth="0" |
|---|
| 26 | minHeight="0" |
|---|
| 27 | height="20" |
|---|
| 28 | layoutClass="{HorizontalFlowLayout}" |
|---|
| 29 | creationComplete="onCreationComplete(event)" |
|---|
| 30 | > |
|---|
| 31 | <mx:Button |
|---|
| 32 | id="playButton" |
|---|
| 33 | icon="@Embed(source='gfx/control_play.png')" |
|---|
| 34 | overIcon="@Embed(source='gfx/control_play_blue.png')" |
|---|
| 35 | skin="{null}" |
|---|
| 36 | width="18" |
|---|
| 37 | height="18" |
|---|
| 38 | verticalCenter="0" |
|---|
| 39 | click="onPlayPauseClick(event)" |
|---|
| 40 | /> |
|---|
| 41 | <mx:Button |
|---|
| 42 | id="pauseButton" |
|---|
| 43 | icon="@Embed(source='gfx/control_pause.png')" |
|---|
| 44 | overIcon="@Embed(source='gfx/control_pause_blue.png')" |
|---|
| 45 | skin="{null}" |
|---|
| 46 | width="18" |
|---|
| 47 | height="18" |
|---|
| 48 | verticalCenter="0" |
|---|
| 49 | click="onPlayPauseClick(event)" |
|---|
| 50 | /> |
|---|
| 51 | <mx:Label |
|---|
| 52 | id="playTimeLabel" |
|---|
| 53 | text="00:00" |
|---|
| 54 | verticalCenter="2" |
|---|
| 55 | /> |
|---|
| 56 | <flexcomponents:ProgressCanvas |
|---|
| 57 | id="progressBar" |
|---|
| 58 | minWidth="10" |
|---|
| 59 | width="100%" |
|---|
| 60 | backgroundColor="0x666666" |
|---|
| 61 | fillColor="0x999999" |
|---|
| 62 | > |
|---|
| 63 | <mx:HSlider |
|---|
| 64 | id="playTimeSlider" |
|---|
| 65 | minimum="0" |
|---|
| 66 | maximum="1" |
|---|
| 67 | minWidth="10" |
|---|
| 68 | trackSkin="{SliderTrackSkin}" |
|---|
| 69 | width="100%" |
|---|
| 70 | horizontalCenter="0" |
|---|
| 71 | verticalCenter="-2" |
|---|
| 72 | dataTipFormatFunction="formatTime" |
|---|
| 73 | liveDragging="false" |
|---|
| 74 | change="onPlayTimeChange(event)" |
|---|
| 75 | thumbPress="onPlayTimePress(event)" |
|---|
| 76 | thumbRelease="onPlayTimeRelease(event)" |
|---|
| 77 | /> |
|---|
| 78 | </flexcomponents:ProgressCanvas> |
|---|
| 79 | <mx:Label |
|---|
| 80 | id="totalTimeLabel" |
|---|
| 81 | text="00:00" |
|---|
| 82 | verticalCenter="2" |
|---|
| 83 | /> |
|---|
| 84 | <mx:HSlider |
|---|
| 85 | id="volumeSlider" |
|---|
| 86 | dataTipFormatFunction="formatVolume" |
|---|
| 87 | minWidth="10" |
|---|
| 88 | width="60" |
|---|
| 89 | minimum="0" |
|---|
| 90 | maximum="1" |
|---|
| 91 | value="100" |
|---|
| 92 | liveDragging="true" |
|---|
| 93 | change="onVolumeChange(event)" |
|---|
| 94 | verticalCenter="-6" |
|---|
| 95 | /> |
|---|
| 96 | <mx:Button |
|---|
| 97 | id="fullscreenButton" |
|---|
| 98 | icon="@Embed(source='gfx/arrow_out.png')" |
|---|
| 99 | overIcon="@Embed(source='gfx/arrow_out.png')" |
|---|
| 100 | width="20" |
|---|
| 101 | height="20" |
|---|
| 102 | skin="{null}" |
|---|
| 103 | verticalCenter="0" |
|---|
| 104 | click="onFullscreenClick(event)" |
|---|
| 105 | /> |
|---|
| 106 | </flexcomponents:LayoutCanvas> |
|---|
| 107 | <fx:Script> |
|---|
| 108 | <![CDATA[ |
|---|
| 109 | import mx.core.mx_internal; |
|---|
| 110 | |
|---|
| 111 | import org.korsakow.flexcomponents.layout.HorizontalFlowLayout; |
|---|
| 112 | import org.korsakow.flexcomponents.skin.SliderTrackSkin; |
|---|
| 113 | import org.korsakow.player.engine.IEnvironment; |
|---|
| 114 | import org.korsakow.player.event.MediaEvent; |
|---|
| 115 | public var mediaAreaId:Number; |
|---|
| 116 | public var showPlayPause:Boolean = true; |
|---|
| 117 | public var showPlayTime:Boolean = true; |
|---|
| 118 | public var showScrubber:Boolean = true; |
|---|
| 119 | public var showLoading:Boolean = true; |
|---|
| 120 | public var showTotalTime:Boolean = true; |
|---|
| 121 | public var showProgress:Boolean = true; |
|---|
| 122 | public var showVolume:Boolean = true; |
|---|
| 123 | public var showFullscreen:Boolean = true; |
|---|
| 124 | private var _mediaArea:IMediaArea; |
|---|
| 125 | private var _prevState:MediaState; |
|---|
| 126 | private function onCreationComplete(event:Event):void |
|---|
| 127 | { |
|---|
| 128 | HorizontalFlowLayout(canvas.layout).paddingX = 5; |
|---|
| 129 | } |
|---|
| 130 | override public function initializeWidget(env:IEnvironment):void |
|---|
| 131 | { |
|---|
| 132 | super.initializeWidget(env); |
|---|
| 133 | var widgets:Vector.<IWidgetComponent> = _env.currentInterface.widgetComponents; |
|---|
| 134 | for each (var widget:IWidgetComponent in widgets) { |
|---|
| 135 | if (widget.model.id == mediaAreaId) { |
|---|
| 136 | _mediaArea = widget as IMediaArea; |
|---|
| 137 | break; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | if (!_mediaArea) |
|---|
| 141 | _mediaArea = env.currentMainMedia; |
|---|
| 142 | |
|---|
| 143 | this.enabled = false; |
|---|
| 144 | if (!_mediaArea) |
|---|
| 145 | return; |
|---|
| 146 | switch (_mediaArea.mediaState) |
|---|
| 147 | { |
|---|
| 148 | case MediaState.NONE: |
|---|
| 149 | break; |
|---|
| 150 | default: |
|---|
| 151 | this.enabled = true; |
|---|
| 152 | playTimeSlider.maximum = _mediaArea.duration; |
|---|
| 153 | totalTimeLabel.text = formatTime(""+_mediaArea.duration); |
|---|
| 154 | break; |
|---|
| 155 | } |
|---|
| 156 | playTimeSlider.value = 0; |
|---|
| 157 | |
|---|
| 158 | playButton.includeInLayout = |
|---|
| 159 | playButton.visible = showPlayPause; |
|---|
| 160 | pauseButton.includeInLayout = |
|---|
| 161 | pauseButton.visible = showPlayPause; |
|---|
| 162 | playTimeLabel.includeInLayout = |
|---|
| 163 | playTimeLabel.visible = showPlayTime; |
|---|
| 164 | playTimeSlider.includeInLayout = |
|---|
| 165 | playTimeSlider.visible = showScrubber; |
|---|
| 166 | totalTimeLabel.includeInLayout = |
|---|
| 167 | totalTimeLabel.visible = showTotalTime; |
|---|
| 168 | if (showProgress) { |
|---|
| 169 | progressBar.backgroundColor = 0x666666; |
|---|
| 170 | progressBar.fillColor = 0x999999; |
|---|
| 171 | } else { |
|---|
| 172 | progressBar.backgroundColor = NaN; |
|---|
| 173 | progressBar.fillColor = NaN; |
|---|
| 174 | } |
|---|
| 175 | volumeSlider.includeInLayout = |
|---|
| 176 | volumeSlider.visible = showVolume; |
|---|
| 177 | fullscreenButton.includeInLayout = |
|---|
| 178 | fullscreenButton.visible = showFullscreen; |
|---|
| 179 | |
|---|
| 180 | _mediaArea.addEventListener(MediaEvent.MEDIA_PLAYBACK_STATE, onPlaybackState); |
|---|
| 181 | _mediaArea.addEventListener(MediaEvent.MEDIA_PLAYTIME_UPDATE, onPlaybackUpdate); |
|---|
| 182 | _mediaArea.addEventListener(MediaEvent.MEDIA_PLAYBACK_BEGIN, onPlaybackBegin); |
|---|
| 183 | _mediaArea.addEventListener(MediaEvent.MEDIA_LOAD_PROGRESS, onLoadProgress); |
|---|
| 184 | } |
|---|
| 185 | override public function destroyWidget(env:IEnvironment):void |
|---|
| 186 | { |
|---|
| 187 | this.enabled = false; |
|---|
| 188 | if (_mediaArea) { |
|---|
| 189 | _mediaArea.removeEventListener(MediaEvent.MEDIA_PLAYBACK_STATE, onPlaybackState); |
|---|
| 190 | _mediaArea.removeEventListener(MediaEvent.MEDIA_PLAYTIME_UPDATE, onPlaybackUpdate); |
|---|
| 191 | _mediaArea.removeEventListener(MediaEvent.MEDIA_PLAYBACK_BEGIN, onPlaybackBegin); |
|---|
| 192 | _mediaArea.removeEventListener(MediaEvent.MEDIA_LOAD_PROGRESS, onLoadProgress); |
|---|
| 193 | _mediaArea = null; |
|---|
| 194 | } |
|---|
| 195 | super.destroyWidget(env); |
|---|
| 196 | } |
|---|
| 197 | public function playpause():void |
|---|
| 198 | { |
|---|
| 199 | onPlayPauseClick(); |
|---|
| 200 | } |
|---|
| 201 | private function onPlaybackState(event:MediaEvent):void |
|---|
| 202 | { |
|---|
| 203 | switch (event.mediaState) |
|---|
| 204 | { |
|---|
| 205 | case MediaState.PLAYING: |
|---|
| 206 | playButton.includeInLayout = |
|---|
| 207 | playButton.visible = false; |
|---|
| 208 | pauseButton.includeInLayout = |
|---|
| 209 | pauseButton.visible = true && showPlayPause; |
|---|
| 210 | break; |
|---|
| 211 | case MediaState.NONE: |
|---|
| 212 | this.enabled = false; |
|---|
| 213 | break; |
|---|
| 214 | default: |
|---|
| 215 | playButton.includeInLayout = |
|---|
| 216 | playButton.visible = true && showPlayPause; |
|---|
| 217 | pauseButton.includeInLayout = |
|---|
| 218 | pauseButton.visible = false; |
|---|
| 219 | playTimeSlider.maximum = _mediaArea.duration; |
|---|
| 220 | totalTimeLabel.text = formatTime(""+_mediaArea.duration); |
|---|
| 221 | this.enabled = true; |
|---|
| 222 | break; |
|---|
| 223 | } |
|---|
| 224 | canvas.invalidateDisplayList(); |
|---|
| 225 | } |
|---|
| 226 | private function onPlaybackBegin(event:Event):void |
|---|
| 227 | { |
|---|
| 228 | } |
|---|
| 229 | private function onPlaybackUpdate(event:Event):void |
|---|
| 230 | { |
|---|
| 231 | playTimeSlider.maximum = _mediaArea.duration; |
|---|
| 232 | playTimeSlider.value = _mediaArea.playTime; |
|---|
| 233 | playTimeLabel.text = formatTime(""+_mediaArea.playTime); |
|---|
| 234 | totalTimeLabel.text = formatTime(""+_mediaArea.duration); |
|---|
| 235 | } |
|---|
| 236 | private function onPlayTimeChange(event:Event):void |
|---|
| 237 | { |
|---|
| 238 | if (!_mediaArea) |
|---|
| 239 | return; |
|---|
| 240 | if (playTimeSlider.mx_internal::invalidatePropertiesFlag) // bug in flex where programmatic changes during invalidation are dispatched as UI events |
|---|
| 241 | return; |
|---|
| 242 | _mediaArea.playTime = playTimeSlider.value; |
|---|
| 243 | } |
|---|
| 244 | private function onPlayTimePress(event:Event):void |
|---|
| 245 | { |
|---|
| 246 | if (!_mediaArea) |
|---|
| 247 | return; |
|---|
| 248 | _prevState = _mediaArea.mediaState; |
|---|
| 249 | _mediaArea.pause(); |
|---|
| 250 | _mediaArea.removeEventListener(MediaEvent.MEDIA_PLAYTIME_UPDATE, onPlaybackUpdate); |
|---|
| 251 | } |
|---|
| 252 | private function onPlayTimeRelease(event:Event):void |
|---|
| 253 | { |
|---|
| 254 | if (!_mediaArea) |
|---|
| 255 | return; |
|---|
| 256 | _mediaArea.addEventListener(MediaEvent.MEDIA_PLAYTIME_UPDATE, onPlaybackUpdate); |
|---|
| 257 | if (_prevState == MediaState.PLAYING) |
|---|
| 258 | _mediaArea.play(); |
|---|
| 259 | _prevState = null; |
|---|
| 260 | } |
|---|
| 261 | private function onVolumeChange(event:Event):void |
|---|
| 262 | { |
|---|
| 263 | if (!_mediaArea) |
|---|
| 264 | return; |
|---|
| 265 | if (playTimeSlider.mx_internal::invalidatePropertiesFlag) // bug in flex where programmatic changes during invalidation are dispatched as UI events |
|---|
| 266 | return; |
|---|
| 267 | _env.soundManager.masterVolume = volumeSlider.value; |
|---|
| 268 | } |
|---|
| 269 | private function onLoadProgress(event:Event):void |
|---|
| 270 | { |
|---|
| 271 | if (_mediaArea.bytesTotal) |
|---|
| 272 | progressBar.position = _mediaArea.bytesLoaded / _mediaArea.bytesTotal; |
|---|
| 273 | } |
|---|
| 274 | private static function formatVolume(str:String):String |
|---|
| 275 | { |
|---|
| 276 | var x:Number = parseFloat(str); |
|---|
| 277 | return "" + int(x*100); |
|---|
| 278 | } |
|---|
| 279 | private static function formatTime(str:String):String |
|---|
| 280 | { |
|---|
| 281 | var x:uint = parseInt(str); |
|---|
| 282 | var ms:String = ''+x % 1000; |
|---|
| 283 | while (ms.length < 3) |
|---|
| 284 | ms = '0' + ms; |
|---|
| 285 | x /= 1000; |
|---|
| 286 | var s:String = ''+x % 60; |
|---|
| 287 | while (s.length < 2) |
|---|
| 288 | s = '0' + s; |
|---|
| 289 | x /= 60; |
|---|
| 290 | var m:String = ''+x;// % 60; |
|---|
| 291 | while (m.length < 2) |
|---|
| 292 | m = '0' + m; |
|---|
| 293 | // x /= 60; |
|---|
| 294 | // var h:String = ''+x % 60; |
|---|
| 295 | // while (h.length < 2) |
|---|
| 296 | // h = '0' + h; |
|---|
| 297 | var tip:String = [m,s].join(':'); |
|---|
| 298 | return tip; |
|---|
| 299 | } |
|---|
| 300 | private function onPlayPauseClick(event:Event=null):void |
|---|
| 301 | { |
|---|
| 302 | if (!_mediaArea) |
|---|
| 303 | return; |
|---|
| 304 | if (_mediaArea.isPlaying) |
|---|
| 305 | _mediaArea.pause(); |
|---|
| 306 | else |
|---|
| 307 | _mediaArea.play(); |
|---|
| 308 | } |
|---|
| 309 | private function onFullscreenClick(event:Event):void |
|---|
| 310 | { |
|---|
| 311 | switch (stage.displayState) |
|---|
| 312 | { |
|---|
| 313 | case StageDisplayState.FULL_SCREEN: |
|---|
| 314 | stage.displayState = StageDisplayState.NORMAL; |
|---|
| 315 | break; |
|---|
| 316 | case StageDisplayState.NORMAL: |
|---|
| 317 | stage.displayState = StageDisplayState.FULL_SCREEN; |
|---|
| 318 | break; |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | ]]> |
|---|
| 322 | </fx:Script> |
|---|
| 323 | </widget:AbstractWidgetComponent> |
|---|