source: trunk/fl5/src/com/longtailvideo/jwplayer/model/PlaylistItem.as @ 1267

Revision 1267, 5.4 KB checked in by pablo, 3 years ago (diff)
  • Addresses a number of "sound" MediaProvider issues, including 928, 976, 982 and 1012. These tickets mostly relate to audio files with no content-length headers, or live audio streams.
  • Fixes issue with HTTP MediaProvider and the "streamer" option (984)
  • Only dispatch "JWPLAYER_MEDIA_LOADED" event for HTTP mediaprovider on item's first load.
  • Fixes issue with start & duration introduced in last check-in
  • More user-friendly error messages for HTTP status errors (1024)
  • Fixes an error with playlist roll-over states
Line 
1package com.longtailvideo.jwplayer.model {
2        import com.longtailvideo.jwplayer.utils.Strings;
3
4        /**
5         * Playlist item data.  The class is dynamic; any items parsed from the jwplayer XML namespace are added to the item.
6         * 
7         * @author Pablo Schklowsky
8         */
9        public dynamic class PlaylistItem {
10                public var author:String                = "";
11                public var date:String                  = "";
12                public var description:String   = "";
13                public var image:String                 = "";
14                public var link:String                  = "";
15                public var mediaid:String               = "";
16                public var tags:String                  = "";
17                public var title:String                 = "";
18                public var provider:String              = "";
19               
20                protected var _file:String                      = "";
21                protected var _streamer:String          = "";
22                protected var _duration:Number          = -1;
23                protected var _start:Number                     = 0;
24               
25                protected var _currentLevel:Number      = -1;
26                protected var _levels:Array                     = [];
27               
28               
29                public function PlaylistItem(obj:Object = null) {
30                        for (var itm:String in obj) {
31                                if (itm == "levels" && obj[itm] is Array) {
32                                        var levels:Array = obj[itm] as Array;
33                                        for each (var level:Object in levels) {
34                                                if (level['file'] && (level['bitrate'] || level['width'])) {
35                                                        addLevel(
36                                                                new PlaylistItemLevel(level['file'],
37                                                                        Number(level['bitrate']),
38                                                                        Number(level['width']),
39                                                                        level['streamer']));
40                                                }
41                                        }
42                                } else {
43                                        this[itm] = obj[itm];
44                                }
45                        }
46                }
47
48                /** File property is now a getter, to take levels into account **/
49                public function get file():String {
50                        if (_levels.length > 0 && _currentLevel > -1 && _currentLevel < _levels.length) {
51                                var level:PlaylistItemLevel = _levels[_currentLevel] as PlaylistItemLevel;
52                                return level.file ? level.file : _file;
53                        } else {
54                                return _file;
55                        }
56                }
57               
58                /** File setter.  Note, if levels are defined, this will be ignored. **/
59                public function set file(f:String):void {
60                        _file = f;
61                }
62               
63                /** Streamer property is now a getter, to take levels into account **/
64                public function get streamer():String {
65                        if (_levels.length > 0 && _currentLevel > -1 && _currentLevel < _levels.length) {
66                                var level:PlaylistItemLevel = _levels[_currentLevel] as PlaylistItemLevel;
67                                return level.streamer ? level.streamer : _streamer;
68                        } else {
69                                return _streamer;
70                        }
71                }
72               
73                /** Streamer setter.  Note, if levels are defined, this will be ignored. **/
74                public function set streamer(s:String):void {
75                        _streamer = s;
76                }
77               
78                /** The quality levels associated with this playlist item **/
79                public function get levels():Array {
80                        return _levels;
81                }
82               
83                /** Insert an additional bitrate level, keeping the array sorted from highest to lowest. **/
84                public function addLevel(newLevel:PlaylistItemLevel):void {
85                        if (validExtension(newLevel.file)) {
86
87                                if (_currentLevel < 0) _currentLevel = 0;
88                                for (var i:Number = 0; i < _levels.length; i++) {
89                                        var level:PlaylistItemLevel = _levels[i] as PlaylistItemLevel;
90                                        if (newLevel.bitrate > level.bitrate) {
91                                                _levels.splice(i, 0, newLevel);
92                                                return;
93                                        } else if (newLevel.bitrate == level.bitrate && newLevel.width > level.width) {
94                                                _levels.splice(i, 0, newLevel);
95                                                return;
96                                        }
97                                }
98                                _levels.push(newLevel);
99                        }
100                }
101
102
103                /** Blacklist a level from usage (e.g. if it cannot be played or drops too many frames). **/
104                public function blacklistLevel(level:Number,state:Boolean=true):void {
105                        if(levels[level]) {
106                                levels[level].blacklisted = state;
107                        }
108                };
109
110
111                /**
112                 * Determines whether this file extension can be played in the Flash player.  If not, ignore the level.
113                 * This is useful for unified HTML5 / Flash failover setups.
114                 **/
115                protected function validExtension(filename:String):Boolean {
116                        var foo:String = Strings.extension(filename);
117                        switch(Strings.extension(filename)) {
118                                case "ogv":
119                                case "ogg":
120                                case "webm":
121                                        return false;
122                                default:
123                                        return true;
124                        }
125                }
126
127                public function get currentLevel():Number {
128                        return _currentLevel;
129                }
130               
131                public function getLevel(bitrate:Number, width:Number):Number {
132                        for (var i:Number=0; i < _levels.length; i++) {
133                                var level:PlaylistItemLevel = _levels[i] as PlaylistItemLevel;
134                                if (bitrate >= level.bitrate * 1.2 && width >= level.width * 0.8 && !level.blacklisted) {
135                                        return i;
136                                }
137                        }
138                        return _levels.length - 1;
139                }
140               
141                /** Set this PlaylistItem's level to match the given bitrate and height. **/
142                public function setLevel(newLevel:Number):void {
143                        if (newLevel >= 0 && newLevel < _levels.length) {
144                                _currentLevel = newLevel;
145                        } else {
146                                throw(new Error("Level index out of bounds"));
147                        }
148                }
149               
150                public function toString():String {
151                        return "[PlaylistItem" +
152                                (this.file ? " file=" + this.file : "") +
153                                (this.streamer ? " streamer=" + this.streamer : "") +
154                                (this.provider ? " provider=" + this.provider : "") +
155                                (this.levels.length ? " level=" + this.currentLevel.toString() : "") +
156                                "]";
157                       
158                }
159               
160               
161                public function get start():Number { return _start; }
162                public function set start(s:*):void {
163                        _start = Strings.seconds(String(s));
164                        if (_start > _duration && _duration > 0) {
165                                _duration += _start;
166                        }
167                }
168
169                public function get duration():Number { return _duration; }
170                public function set duration(d:*):void {
171                        _duration = Strings.seconds(String(d));
172                        if (_start > _duration && _duration > 0) {
173                                _duration += _start;
174                        }
175                }
176               
177                // For backwards compatibility
178                public function get type():String { return provider; }
179                public function set type(t:String):void { provider = t; }
180               
181        }
182}
Note: See TracBrowser for help on using the repository browser.