source: trunk/fl5/src/com/longtailvideo/jwplayer/media/ImageMediaProvider.as @ 837

Revision 837, 3.5 KB checked in by pablo, 3 years ago (diff)

Fix ImageMediaProvider (Broken in 835)

Line 
1/**
2 * Model for playback of GIF/JPG/PNG images.
3 **/
4package com.longtailvideo.jwplayer.media {
5        import com.jeroenwijering.events.*;
6        import com.longtailvideo.jwplayer.events.MediaEvent;
7        import com.longtailvideo.jwplayer.model.PlayerConfig;
8        import com.longtailvideo.jwplayer.model.PlaylistItem;
9        import com.longtailvideo.jwplayer.player.PlayerState;
10        import com.longtailvideo.jwplayer.utils.Draw;
11        import com.longtailvideo.jwplayer.utils.Logger;
12       
13        import flash.display.*;
14        import flash.events.*;
15        import flash.net.URLRequest;
16        import flash.system.LoaderContext;
17        import flash.utils.*;
18
19
20        public class ImageMediaProvider extends MediaProvider {
21                /** Loader that loads the image. **/
22                private var _loader:Loader;
23                /** ID for the position _postitionInterval. **/
24                private var _postitionInterval:Number;
25
26
27                /** Constructor; sets up listeners **/
28                public function ImageMediaProvider() {
29                        super('image');
30                }
31
32
33                public override function initializeMediaProvider(cfg:PlayerConfig):void {
34                        super.initializeMediaProvider(cfg);
35                        _loader = new Loader();
36                        _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderHandler);
37                        _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
38                        _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
39                }
40
41
42                /** load image into screen **/
43                override public function load(itm:PlaylistItem):void {
44                        _item = itm;
45                        _position = 0;
46                        _loader.load(new URLRequest(item.file), new LoaderContext(true));
47                        setState(PlayerState.BUFFERING);
48                        sendBufferEvent(0);
49                }
50
51
52                /** Catch errors. **/
53                private function errorHandler(evt:ErrorEvent):void {
54                        stop();
55                        error(evt.text);
56                }
57
58
59                /** Load and place the image on stage. **/
60                private function loaderHandler(evt:Event):void {
61                        media = _loader;
62                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED);
63                        try {
64                                Draw.smooth(_loader.content as Bitmap);
65                        } catch (e:Error) {
66                                Logger.log("Could not smooth image file: " + e.message);
67                        }
68                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: {height: evt.target.height, width: evt.target.width}});
69                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL);
70                }
71
72
73                /** Pause playback of the_item. **/
74                override public function pause():void {
75                        clearInterval(_postitionInterval);
76                        super.pause();
77                }
78
79
80                /** Resume playback of the_item. **/
81                override public function play():void {
82                        _postitionInterval = setInterval(positionInterval, 100);
83                        super.play();
84                }
85
86
87                /** Interval function that pings the _position. **/
88                protected function positionInterval():void {
89                        if (state != PlayerState.PLAYING) { return; }
90
91                        _position = Math.round(position * 10 + 1) / 10;
92                        if (position < _item.duration) {
93                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: position, duration: item.duration});
94                        } else if (_item.duration > 0) {
95                                complete();
96                        }
97                }
98
99
100                /** Send load progress to player. **/
101                private function progressHandler(evt:ProgressEvent):void {
102                        var pct:Number = Math.round(evt.bytesLoaded / evt.bytesTotal * 100);
103                        sendBufferEvent(pct);
104                }
105
106
107                /** Seek to a certain _position in the_item. **/
108                override public function seek(pos:Number):void {
109                        clearInterval(_postitionInterval);
110                        _position = pos;
111                        play();
112                }
113
114
115                /** Stop the image _postitionInterval. **/
116                override public function stop():void {
117                        if (_loader.contentLoaderInfo.bytesLoaded != _loader.contentLoaderInfo.bytesTotal) {
118                                _loader.close();
119                        } else {
120                                _loader.unload();
121                        }
122                        clearInterval(_postitionInterval);
123                        super.stop();
124                }
125        }
126}
Note: See TracBrowser for help on using the repository browser.