| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 0 | package org.vostokframework.domain.loading.states.fileloader.adapters |
| 30 | |
{ |
| 31 | |
import org.vostokframework.domain.loading.states.fileloader.IDataLoader; |
| 32 | |
import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmMediaEvent; |
| 33 | |
|
| 34 | |
import flash.events.Event; |
| 35 | |
import flash.events.ProgressEvent; |
| 36 | |
import flash.events.TimerEvent; |
| 37 | |
import flash.net.NetStream; |
| 38 | |
import flash.utils.Timer; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public class ProgressNetStream extends DataLoaderBehavior |
| 46 | |
{ |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private var _bufferCompleteEventDispatched:Boolean; |
| 51 | |
private var _completeEventDispatched:Boolean; |
| 52 | |
private var _netStream:NetStream; |
| 53 | |
private var _openEventDispatched:Boolean; |
| 54 | |
private var _percentToBuffer:int; |
| 55 | |
private var _timerProgress:Timer; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public function ProgressNetStream(wrappedDataLoader:IDataLoader, netStream:NetStream, percentToBuffer:int = 0) |
| 65 | |
{ |
| 66 | 0 | super(wrappedDataLoader); |
| 67 | |
|
| 68 | 0 | if (!netStream) throw new ArgumentError("Argument <netStream> must not be null."); |
| 69 | |
|
| 70 | 0 | _netStream = netStream; |
| 71 | 0 | _percentToBuffer = percentToBuffer; |
| 72 | |
|
| 73 | 0 | _timerProgress = new Timer(50); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
override public function cancel(): void |
| 77 | |
{ |
| 78 | 0 | stopTimer(); |
| 79 | 0 | super.cancel(); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
override public function load(): void |
| 86 | |
{ |
| 87 | 0 | super.load(); |
| 88 | 0 | startTimer(); |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
override public function stop():void |
| 95 | |
{ |
| 96 | 0 | stopTimer(); |
| 97 | 0 | _completeEventDispatched = false; |
| 98 | 0 | _bufferCompleteEventDispatched = false; |
| 99 | 0 | _openEventDispatched = false; |
| 100 | |
|
| 101 | 0 | super.stop(); |
| 102 | 0 | } |
| 103 | |
|
| 104 | |
override protected function doDispose():void |
| 105 | |
{ |
| 106 | 0 | stopTimer(); |
| 107 | |
|
| 108 | 0 | _netStream = null; |
| 109 | 0 | _timerProgress = null; |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
private function dispatchProgressEvent():void |
| 113 | |
{ |
| 114 | 0 | _netStream.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _netStream.bytesLoaded, _netStream.bytesTotal)); |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
private function loadingBufferComplete():void |
| 118 | |
{ |
| 119 | 0 | _bufferCompleteEventDispatched = true; |
| 120 | 0 | dispatchProgressEvent(); |
| 121 | 0 | _netStream.dispatchEvent(new FileLoadingAlgorithmMediaEvent(FileLoadingAlgorithmMediaEvent.BUFFER_COMPLETE)); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
private function loadingComplete():void |
| 125 | |
{ |
| 126 | 0 | stopTimer(); |
| 127 | 0 | _completeEventDispatched = true; |
| 128 | |
|
| 129 | |
|
| 130 | 0 | _netStream.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _netStream.bytesTotal, _netStream.bytesTotal)); |
| 131 | 0 | _netStream.dispatchEvent(new Event(Event.COMPLETE)); |
| 132 | 0 | } |
| 133 | |
|
| 134 | |
private function loadingOpen():void |
| 135 | |
{ |
| 136 | 0 | _openEventDispatched = true; |
| 137 | 0 | _netStream.dispatchEvent(new Event(Event.OPEN)); |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
private function progressHandler(event:TimerEvent):void |
| 141 | |
{ |
| 142 | 0 | var bufferBytesTotal:int = (_netStream.bytesTotal * _percentToBuffer) / 100; |
| 143 | 0 | var currentBufferPercent:int = (_netStream.bytesTotal > 0) ? Math.floor((_netStream.bytesLoaded * 100) / bufferBytesTotal) : 0; |
| 144 | |
|
| 145 | 0 | if (!_openEventDispatched) |
| 146 | |
{ |
| 147 | 0 | loadingOpen(); |
| 148 | |
} |
| 149 | 0 | else if (_percentToBuffer > 0 && currentBufferPercent >= _percentToBuffer && !_bufferCompleteEventDispatched) |
| 150 | |
{ |
| 151 | 0 | loadingBufferComplete(); |
| 152 | |
} |
| 153 | 0 | else if (_netStream.bytesLoaded == _netStream.bytesTotal && _netStream.bytesLoaded > 0 && !_completeEventDispatched) |
| 154 | |
{ |
| 155 | 0 | loadingComplete(); |
| 156 | |
} |
| 157 | |
else |
| 158 | |
{ |
| 159 | 0 | dispatchProgressEvent(); |
| 160 | |
} |
| 161 | 0 | } |
| 162 | |
|
| 163 | |
private function startTimer():void |
| 164 | |
{ |
| 165 | 0 | _timerProgress.addEventListener(TimerEvent.TIMER, progressHandler, false, 0, true); |
| 166 | 0 | _timerProgress.start(); |
| 167 | 0 | } |
| 168 | |
|
| 169 | |
private function stopTimer():void |
| 170 | |
{ |
| 171 | 0 | _timerProgress.stop(); |
| 172 | 0 | _timerProgress.reset(); |
| 173 | 0 | _timerProgress.removeEventListener(TimerEvent.TIMER, progressHandler, false); |
| 174 | 0 | } |
| 175 | |
|
| 176 | |
} |
| 177 | |
|
| 178 | |
} |