| 1 | package com.longtailvideo.jwplayer.media |
|---|
| 2 | { |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 6 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 7 | import com.longtailvideo.jwplayer.model.PlaylistItem; |
|---|
| 8 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 9 | import com.longtailvideo.jwplayer.utils.Logger; |
|---|
| 10 | import com.longtailvideo.jwplayer.utils.Stretcher; |
|---|
| 11 | |
|---|
| 12 | import flash.display.DisplayObjectContainer; |
|---|
| 13 | import flash.display.Loader; |
|---|
| 14 | import flash.display.MovieClip; |
|---|
| 15 | import flash.events.ErrorEvent; |
|---|
| 16 | import flash.events.Event; |
|---|
| 17 | import flash.events.EventDispatcher; |
|---|
| 18 | import flash.external.ExternalInterface; |
|---|
| 19 | import flash.utils.clearInterval; |
|---|
| 20 | import flash.utils.setInterval; |
|---|
| 21 | import flash.utils.setTimeout; |
|---|
| 22 | |
|---|
| 23 | import org.osmf.containers.MediaContainer; |
|---|
| 24 | import org.osmf.elements.ImageElement; |
|---|
| 25 | import org.osmf.elements.VideoElement; |
|---|
| 26 | import org.osmf.events.BufferEvent; |
|---|
| 27 | import org.osmf.events.DynamicStreamEvent; |
|---|
| 28 | import org.osmf.events.MediaErrorEvent; |
|---|
| 29 | import org.osmf.events.MediaPlayerStateChangeEvent; |
|---|
| 30 | import org.osmf.events.PlayEvent; |
|---|
| 31 | import org.osmf.events.TimeEvent; |
|---|
| 32 | import org.osmf.media.DefaultMediaFactory; |
|---|
| 33 | import org.osmf.media.MediaElement; |
|---|
| 34 | import org.osmf.media.MediaFactory; |
|---|
| 35 | import org.osmf.media.MediaPlayer; |
|---|
| 36 | import org.osmf.media.URLResource; |
|---|
| 37 | import org.osmf.net.DynamicStreamingResource; |
|---|
| 38 | import org.osmf.net.NetLoader; |
|---|
| 39 | import org.osmf.traits.LoadTrait; |
|---|
| 40 | import org.osmf.traits.MediaTraitType; |
|---|
| 41 | import org.osmf.traits.PlayState; |
|---|
| 42 | import org.osmf.utils.OSMFSettings; |
|---|
| 43 | import org.osmf.utils.URL; |
|---|
| 44 | import org.osmf.events.MediaError; |
|---|
| 45 | import org.osmf.events.MediaErrorCodes; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | public class HDSMediaProvider extends MediaProvider { |
|---|
| 50 | /** OSMF player **/ |
|---|
| 51 | protected var _osmfPlayer:MediaPlayer; |
|---|
| 52 | /**OSMF media container**/ |
|---|
| 53 | protected var _container:MediaContainer; |
|---|
| 54 | /**the media element--the subtype is determined by extension**/ |
|---|
| 55 | protected var _element:MediaElement; |
|---|
| 56 | /** ID for the position interval. **/ |
|---|
| 57 | protected var _positionInterval:Number; |
|---|
| 58 | |
|---|
| 59 | public function HDSMediaProvider() { |
|---|
| 60 | //OSMFSettings.enableStageVideo = false; |
|---|
| 61 | _osmfPlayer = new MediaPlayer(); |
|---|
| 62 | _container = new MediaContainer(); |
|---|
| 63 | _osmfPlayer.addEventListener(TimeEvent.COMPLETE, completeEvent); |
|---|
| 64 | _osmfPlayer.addEventListener(MediaErrorEvent.MEDIA_ERROR,errorEvent); |
|---|
| 65 | _osmfPlayer.addEventListener(BufferEvent.BUFFERING_CHANGE,bufferingChangeHandler); |
|---|
| 66 | _osmfPlayer.addEventListener(TimeEvent.CURRENT_TIME_CHANGE,positionHandler); |
|---|
| 67 | _osmfPlayer.addEventListener(DynamicStreamEvent.SWITCHING_CHANGE,handleDynamicChange); |
|---|
| 68 | super("hds"); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Load a new playlist item |
|---|
| 73 | * @param itm The playlistItem to load |
|---|
| 74 | **/ |
|---|
| 75 | public override function load(itm:PlaylistItem):void { |
|---|
| 76 | _item = itm; |
|---|
| 77 | |
|---|
| 78 | var resource:URLResource = new URLResource( _item.file ); |
|---|
| 79 | |
|---|
| 80 | var mediaFactory:DefaultMediaFactory = new DefaultMediaFactory(); |
|---|
| 81 | //media factor associates the extension with the right element type. If the extension isn't supported or recognized, returns null |
|---|
| 82 | _element = mediaFactory.createMediaElement( resource ); |
|---|
| 83 | if (_element == null) { |
|---|
| 84 | super.load(item); |
|---|
| 85 | error("Unsupported File Type"); |
|---|
| 86 | return; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | media = _container; |
|---|
| 90 | _container.addMediaElement(_element); |
|---|
| 91 | super.load(itm); |
|---|
| 92 | _osmfPlayer.volume = config.mute ? 0 : config.volume; |
|---|
| 93 | _osmfPlayer.media = _element; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * handles the position of the status bar |
|---|
| 99 | * |
|---|
| 100 | **/ |
|---|
| 101 | protected function positionHandler(event:TimeEvent):void { |
|---|
| 102 | |
|---|
| 103 | var pos:Number = Math.round(Math.min(_osmfPlayer.currentTime, Math.max(_osmfPlayer.duration, 0)) * 100) / 100; |
|---|
| 104 | var timeRemaining:Number = _osmfPlayer.duration > 0 ? (_osmfPlayer.duration - _osmfPlayer.currentTime) : _osmfPlayer.currentTime; |
|---|
| 105 | |
|---|
| 106 | if (state != PlayerState.PLAYING) { |
|---|
| 107 | return; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | _position = pos; |
|---|
| 111 | |
|---|
| 112 | if (position < _osmfPlayer.duration) { |
|---|
| 113 | if (position >= 0) { |
|---|
| 114 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: position, duration: _osmfPlayer.duration}); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * handles buffer change state. dispatches the needed events |
|---|
| 121 | * @param the buffer change event |
|---|
| 122 | **/ |
|---|
| 123 | private function bufferingChangeHandler(event:BufferEvent): void |
|---|
| 124 | { |
|---|
| 125 | if (_osmfPlayer.buffering) { |
|---|
| 126 | sendBufferEvent(0); |
|---|
| 127 | setState(PlayerState.BUFFERING); |
|---|
| 128 | } else if (_osmfPlayer.playing) { |
|---|
| 129 | |
|---|
| 130 | dispatchEvent(new MediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL)); |
|---|
| 131 | _item.duration = _osmfPlayer.duration; |
|---|
| 132 | //fixes size issue |
|---|
| 133 | resize(_width,_height); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * listen for end of file |
|---|
| 139 | * @param the play state change event |
|---|
| 140 | **/ |
|---|
| 141 | private function completeEvent(event:TimeEvent):void { |
|---|
| 142 | complete(); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * listen for dynamic stream change and resize |
|---|
| 148 | * @param dynamic stream change event |
|---|
| 149 | **/ |
|---|
| 150 | private function handleDynamicChange(event:DynamicStreamEvent):void { |
|---|
| 151 | resize(_width,_height); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * listen for errors |
|---|
| 157 | * @param the error event |
|---|
| 158 | **/ |
|---|
| 159 | private function errorEvent(event:MediaErrorEvent):void { |
|---|
| 160 | Logger.log(event.error.detail); |
|---|
| 161 | error("OSMF Playback Error"); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** Resume playback of the item. **/ |
|---|
| 165 | public override function play():void { |
|---|
| 166 | if (_osmfPlayer.paused) { |
|---|
| 167 | _osmfPlayer.play(); |
|---|
| 168 | } |
|---|
| 169 | super.play(); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** Pause playback of the item. **/ |
|---|
| 173 | public override function pause():void { |
|---|
| 174 | if (_osmfPlayer.playing) { |
|---|
| 175 | _osmfPlayer.pause(); |
|---|
| 176 | } |
|---|
| 177 | super.pause(); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * Seek to a certain position in the item. |
|---|
| 183 | * |
|---|
| 184 | * @param pos The position in seconds. |
|---|
| 185 | **/ |
|---|
| 186 | /** Seek to a new position. **/ |
|---|
| 187 | override public function seek(pos:Number):void { |
|---|
| 188 | |
|---|
| 189 | _osmfPlayer.seek(pos); |
|---|
| 190 | |
|---|
| 191 | //jw should restart on seek |
|---|
| 192 | if (_osmfPlayer.paused) { |
|---|
| 193 | play(); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** Stop playing and loading the item. **/ |
|---|
| 199 | public override function stop():void { |
|---|
| 200 | if (_osmfPlayer.playing) { |
|---|
| 201 | _osmfPlayer.stop(); |
|---|
| 202 | } |
|---|
| 203 | if (_element != null){ |
|---|
| 204 | _container.removeMediaElement(_element); |
|---|
| 205 | _element = null; |
|---|
| 206 | } |
|---|
| 207 | super.stop(); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * Change the playback volume of the item. |
|---|
| 213 | * |
|---|
| 214 | * @param vol The new volume (0 to 100). |
|---|
| 215 | * note that the osmf volume is [0 to 1] |
|---|
| 216 | **/ |
|---|
| 217 | public override function setVolume(vol:Number):void { |
|---|
| 218 | _osmfPlayer.volume = vol/100; |
|---|
| 219 | super.setVolume(vol); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | } |
|---|