source: trunk/fl5/src/com/longtailvideo/jwplayer/media/SoundMediaProvider.as @ 380

Revision 380, 5.0 KB checked in by pablo, 4 years ago (diff)

MediaState -> PlayerState
PNG Skinning

Line 
1/**
2 * Wrapper for playback of mp3 sounds.
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 flash.events.*;
11        import flash.media.*;
12        import flash.net.URLRequest;
13        import flash.utils.*;
14       
15       
16        public class SoundMediaProvider extends MediaProvider {
17                /** sound object to be instantiated. **/
18                private var sound:Sound;
19                /** Sound control object. **/
20                private var transformer:SoundTransform;
21                /** Sound channel object. **/
22                private var channel:SoundChannel;
23                /** Sound context object. **/
24                private var context:SoundLoaderContext;
25                /** ID for the _position interval. **/
26                protected var interval:Number;
27                /** Interval for loading progress. **/
28                private var loadinterval:uint;
29               
30               
31                /** Constructor; sets up the connection and display. **/
32                public function SoundMediaProvider() {
33               
34                }
35               
36                public override function initializeMediaProvider(cfg:PlayerConfig):void {
37                        super.initializeMediaProvider(cfg);
38                        _provider = 'sound';
39                        transformer = new SoundTransform();
40                        context = new SoundLoaderContext(_config.bufferlength * 1000, true);
41                }
42               
43               
44                /** Sound completed; send event. **/
45                private function completeHandler(evt:Event):void {
46                        clearInterval(interval);
47                        setState(PlayerState.IDLE);
48                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE);
49                }
50               
51               
52                /** Catch errors. **/
53                private function errorHandler(evt:ErrorEvent):void {
54                        stop();
55                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: evt.text});
56                }
57               
58               
59                /** Forward ID3 data from the sound. **/
60                private function id3Handler(evt:Event):void {
61                        try {
62                                var id3:ID3Info = sound.id3;
63                                var obj:Object = {type: 'id3', album: id3.album, artist: id3.artist, comment: id3.comment, genre: id3.genre, name: id3.songName, track: id3.track, year: id3.year}
64                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, obj);
65                        } catch (err:Error) {
66                        }
67                }
68               
69               
70                /** Load the sound. **/
71                override public function load(itm:PlaylistItem):void {
72                        _item = itm;
73                        _position = 0;
74                        sound = new Sound();
75                        sound.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
76                        sound.addEventListener(Event.ID3, id3Handler);
77                        sound.load(new URLRequest(_item.file), context);
78                        play();
79                        if (_item.start > 0) {
80                                seek(_item.start);
81                        }
82                        loadinterval = setInterval(loadHandler, 200);
83                        _config.mute == true ? setVolume(0) : setVolume(_config.volume);
84                        setState(PlayerState.BUFFERING);
85                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {percentage: 0});
86                }
87               
88               
89                /** Interval for the loading progress **/
90                private function loadHandler():void {
91                        var ldd:uint = sound.bytesLoaded;
92                        var ttl:int = sound.bytesTotal;
93                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED, {loaded: ldd, total: ttl});
94                        if (ldd / ttl > 0.1 && _item.duration == 0) {
95                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {duration: sound.length / 1000 / ldd * ttl});
96                        }
97                        if (ldd == ttl && ldd > 0) {
98                                clearInterval(loadinterval);
99                        }
100                }
101               
102               
103                /** Pause the sound. **/
104                override public function pause():void {
105                        channel.stop();
106                        clearInterval(interval);
107                        super.pause();
108                }
109               
110               
111                /** Play the sound. **/
112                override public function play():void {
113                        channel = sound.play(_position * 1000, 0, transformer);
114                        channel.addEventListener(Event.SOUND_COMPLETE, completeHandler);
115                        interval = setInterval(positionInterval, 100);
116                        super.play();
117                }
118               
119               
120                /** Interval for the _position progress **/
121                protected function positionInterval():void {
122                        _position = Math.round(channel.position / 100) / 10;
123                        if (sound.isBuffering == true && sound.bytesTotal > sound.bytesLoaded) {
124                                if (_config.state != PlayerState.BUFFERING) {
125                                        setState(PlayerState.BUFFERING);
126                                } else {
127                                        var pct:Number = Math.floor(sound.length / (channel.position + _config.bufferlength * 1000) * 100);
128                                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {percentage: pct});
129                                }
130                        } else if (_config.state == PlayerState.BUFFERING && sound.isBuffering == false) {
131                                super.play();
132                        }
133                        if (_position < _item.duration) {
134                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {_position: _position, duration: _item.duration});
135                        } else if (_item.duration > 0) {
136                                pause();
137                                setState(PlayerState.IDLE);
138                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE);
139                        }
140                }
141               
142               
143                /** Seek in the sound. **/
144                override public function seek(pos:Number):void {
145                        _position = pos;
146                        clearInterval(interval);
147                        channel.stop();
148                        play();
149                }
150               
151               
152                /** Destroy the sound. **/
153                override public function stop():void {
154                        if (channel) {
155                                channel.stop();
156                        }
157                        try {
158                                sound.close();
159                        } catch (err:Error) {
160                        }
161                        clearInterval(loadinterval);
162                        clearInterval(interval);
163                        super.stop();
164                }
165               
166               
167                /** Set the volume level. **/
168                override public function setVolume(vol:Number):void {
169                        transformer.volume = vol / 100;
170                        if (channel) {
171                                channel.soundTransform = transformer;
172                        }
173                }
174        }
175}
Note: See TracBrowser for help on using the repository browser.