root/trunk/as3/com/jeroenwijering/models/SoundModel.as @ 42

Revision 42, 4.3 kB (checked in by jeroen, 16 months ago)

tagged the 1.1 wmvplayer and started the air player

  • Property svn:executable set to *
Line 
1/**
2* Wrapper for playback of mp3 sounds.
3**/
4package com.jeroenwijering.models {
5
6
7import com.jeroenwijering.events.*;
8import com.jeroenwijering.models.ModelInterface;
9import com.jeroenwijering.player.Model;
10import flash.events.*;
11import flash.media.*;
12import flash.net.URLRequest;
13import flash.utils.clearInterval;
14import flash.utils.setInterval;
15
16
17public class SoundModel implements ModelInterface {
18
19
20        /** reference to the model. **/
21        private var model:Model;
22        /** sound object to be instantiated. **/
23        private var sound:Sound;
24        /** Sound control object. **/
25        private var transform:SoundTransform;
26        /** Sound channel object. **/
27        private var channel:SoundChannel;
28        /** Sound context object. **/
29        private var context:SoundLoaderContext;
30        /** Interval ID for the time. **/
31        private var interval:Number;
32        /** Current position. **/
33        private var position:Number;
34        /** Estimated duration. **/
35        private var duration:Number;
36
37
38        /** Constructor; sets up the connection and display. **/
39        public function SoundModel(mod:Model) {
40                model = mod;
41                transform = new SoundTransform();
42                model.config['mute'] == true ? volume(0): volume(model.config['volume']);
43                context = new SoundLoaderContext(model.config['bufferlength']*1000);
44        };
45
46
47        /** Sound completed; send event. **/
48        private function completeHandler(evt:Event) {
49                clearInterval(interval);
50                position = model.playlist[model.config['item']]['start'];
51                model.sendEvent(ModelEvent.TIME,{position:position,duration:duration});
52                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED});
53        };
54
55
56        /** Catch errors. **/
57        private function errorHandler(evt:ErrorEvent) {
58                model.sendEvent(ModelEvent.ERROR,{message:evt.text});
59                stop();
60        };
61
62
63        /** Load the sound. **/
64        public function load() {
65                position = model.playlist[model.config['item']]['start'];
66                duration = model.playlist[model.config['item']]['duration'];
67                sound = new Sound();
68                sound.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
69                sound.addEventListener(ProgressEvent.PROGRESS,progressHandler);
70                sound.load(new URLRequest(model.playlist[model.config['item']]['file']),context);
71                model.mediaHandler();
72                play();
73        };
74
75
76        /** Pause the sound. **/
77        public function pause() {
78                clearInterval(interval);
79                channel.stop();
80                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED});
81        };
82
83
84        /** Play the sound. **/
85        public function play() {
86                channel = sound.play(position*1000,0,transform);
87                channel.removeEventListener(Event.SOUND_COMPLETE,completeHandler);
88                channel.addEventListener(Event.SOUND_COMPLETE,completeHandler);
89                interval = setInterval(timeHandler,100);
90                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});
91        };
92
93
94        /** Interval for the loading progress **/
95        private function progressHandler(evt:ProgressEvent) {
96                var ldd = evt.bytesLoaded;
97                var ttl = evt.bytesTotal;
98                model.sendEvent(ModelEvent.LOADED,{loaded:ldd,total:ttl});
99        };
100
101
102        /** Change quality setting. **/
103        public function quality(typ:Boolean) {};
104
105
106        /** Seek in the sound. **/
107        public function seek(pos:Number) {
108                clearInterval(interval);
109                position = pos;
110                channel.stop();
111                play();
112        };
113
114
115        /** Destroy the sound. **/
116        public function stop() {
117                clearInterval(interval);
118                if(channel) { channel.stop(); }
119                if(sound.bytesLoaded < sound.bytesTotal) { sound.close(); }
120        };
121
122
123        /** Interval for the position progress **/
124        private function timeHandler() {
125                position = Math.round(channel.position/100)/10;
126                var dur = Math.round(sound.length*sound.bytesTotal/sound.bytesLoaded/100)/10;
127                if(sound.isBuffering == true && sound.bytesTotal > sound.bytesLoaded) {
128                        if(model.config['state'] != ModelStates.BUFFERING) {
129                                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING});
130                        } else {
131                                var pct = Math.floor(sound.length/(channel.position+model.config['bufferlength']*1000)*100);
132                                        model.sendEvent(ModelEvent.BUFFER,{percentage:pct});
133                        }
134                } else if (model.config['state'] == ModelStates.BUFFERING && sound.isBuffering == false) {
135                        model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});
136                }
137                if(dur > position) {
138                        model.sendEvent(ModelEvent.TIME,{position:position,duration:dur});
139                }
140                if(dur != duration && !isNaN(dur)) {
141                        duration = dur;
142                        model.sendEvent(ModelEvent.META,{duration:duration});
143                }
144        };
145
146
147        /** Set the volume level. **/
148        public function volume(vol:Number) {
149                transform.volume = vol/100;
150                if(channel) {
151                        channel.soundTransform = transform;
152                }
153        };
154
155
156};
157
158
159}
Note: See TracBrowser for help on using the browser.