| 1 | /** |
|---|
| 2 | * Wrapper for playback of mp3 sounds. |
|---|
| 3 | **/ |
|---|
| 4 | package 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 | |
|---|
| 11 | import flash.events.*; |
|---|
| 12 | import flash.media.*; |
|---|
| 13 | import flash.net.URLRequest; |
|---|
| 14 | import flash.utils.*; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | public class SoundMediaProvider extends MediaProvider { |
|---|
| 18 | /** _sound object to be instantiated. **/ |
|---|
| 19 | private var _sound:Sound; |
|---|
| 20 | /** Sound control object. **/ |
|---|
| 21 | private var _transformer:SoundTransform; |
|---|
| 22 | /** Sound _channel object. **/ |
|---|
| 23 | private var _channel:SoundChannel; |
|---|
| 24 | /** Sound _context object. **/ |
|---|
| 25 | private var _context:SoundLoaderContext; |
|---|
| 26 | /** ID for the position interval. **/ |
|---|
| 27 | protected var _positionInterval:Number; |
|---|
| 28 | /** Whether the buffer has filled **/ |
|---|
| 29 | private var _bufferFull:Boolean; |
|---|
| 30 | /** Whether the enitre video has been buffered **/ |
|---|
| 31 | private var _bufferingComplete:Boolean; |
|---|
| 32 | |
|---|
| 33 | /** Constructor; sets up the connection and display. **/ |
|---|
| 34 | public function SoundMediaProvider() { |
|---|
| 35 | super('_sound'); |
|---|
| 36 | |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | public override function initializeMediaProvider(cfg:PlayerConfig):void { |
|---|
| 41 | super.initializeMediaProvider(cfg); |
|---|
| 42 | _transformer = new SoundTransform(); |
|---|
| 43 | _context = new SoundLoaderContext(config.bufferlength * 1000, true); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** Sound completed; send event. **/ |
|---|
| 48 | private function completeHandler(evt:Event):void { |
|---|
| 49 | complete(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** Catch errors. **/ |
|---|
| 54 | private function errorHandler(evt:ErrorEvent):void { |
|---|
| 55 | stop(); |
|---|
| 56 | error(evt.text); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /** Forward ID3 data from the _sound. **/ |
|---|
| 61 | private function id3Handler(evt:Event):void { |
|---|
| 62 | try { |
|---|
| 63 | var id3:ID3Info = _sound.id3; |
|---|
| 64 | var obj:Object = {type: 'id3', album: id3.album, |
|---|
| 65 | artist: id3.artist, comment: id3.comment, |
|---|
| 66 | genre: id3.genre, name: id3.songName, track: id3.track, |
|---|
| 67 | year: id3.year} |
|---|
| 68 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, obj); |
|---|
| 69 | } catch (err:Error) { |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /** Load the _sound. **/ |
|---|
| 75 | override public function load(itm:PlaylistItem):void { |
|---|
| 76 | _position = 0; |
|---|
| 77 | _bufferFull = false; |
|---|
| 78 | _bufferingComplete = false; |
|---|
| 79 | if (!_item || _item.file != itm.file) { |
|---|
| 80 | _item = itm; |
|---|
| 81 | _sound = new Sound(); |
|---|
| 82 | _sound.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); |
|---|
| 83 | _sound.addEventListener(Event.ID3, id3Handler); |
|---|
| 84 | _sound.addEventListener(ProgressEvent.PROGRESS, positionHandler); |
|---|
| 85 | _sound.load(new URLRequest(_item.file), _context); |
|---|
| 86 | } |
|---|
| 87 | if (!_positionInterval) { |
|---|
| 88 | _positionInterval = setInterval(positionHandler, 100); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | setState(PlayerState.BUFFERING); |
|---|
| 92 | sendBufferEvent(0); |
|---|
| 93 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED); |
|---|
| 94 | config.mute == true ? setVolume(0) : setVolume(config.volume); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** Pause the _sound. **/ |
|---|
| 99 | override public function pause():void { |
|---|
| 100 | if (_positionInterval){ |
|---|
| 101 | clearInterval(_positionInterval); |
|---|
| 102 | _positionInterval = undefined; |
|---|
| 103 | } |
|---|
| 104 | if (_channel) { |
|---|
| 105 | _channel.stop(); |
|---|
| 106 | } |
|---|
| 107 | super.pause(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /** Play the _sound. **/ |
|---|
| 112 | override public function play():void { |
|---|
| 113 | if (position == 0 && _item.start > 0) { |
|---|
| 114 | seek(item.start); |
|---|
| 115 | return; |
|---|
| 116 | } |
|---|
| 117 | if (!_positionInterval) { |
|---|
| 118 | _positionInterval = setInterval(positionHandler, 100); |
|---|
| 119 | } |
|---|
| 120 | if (_channel){ |
|---|
| 121 | _channel.stop(); |
|---|
| 122 | _channel = null; |
|---|
| 123 | } |
|---|
| 124 | _channel = _sound.play(_position * 1000, 0, _transformer); |
|---|
| 125 | _channel.addEventListener(Event.SOUND_COMPLETE, completeHandler); |
|---|
| 126 | super.play(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /** Interval for the _position progress **/ |
|---|
| 131 | protected function positionHandler(progressEvent:ProgressEvent=null):void { |
|---|
| 132 | var bufferPercent:Number; |
|---|
| 133 | |
|---|
| 134 | if (_sound.bytesLoaded / _sound.bytesTotal > 0.1 && _item.duration <= 0) { |
|---|
| 135 | _item.duration = _sound.length / 1000 / _sound.bytesLoaded * _sound.bytesTotal; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | if (_channel) { |
|---|
| 139 | _position = Math.round(_channel.position / 100) / 10; |
|---|
| 140 | bufferPercent = Math.floor(_sound.bytesLoaded / _sound.bytesTotal * 100); |
|---|
| 141 | } else if (!_channel && progressEvent) { |
|---|
| 142 | bufferPercent = Math.floor(progressEvent.bytesLoaded / progressEvent.bytesTotal * 100); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | if (_sound.isBuffering == true && _sound.bytesTotal > _sound.bytesLoaded) { |
|---|
| 146 | if (state != PlayerState.BUFFERING) { |
|---|
| 147 | _bufferFull = false; |
|---|
| 148 | if (_channel) { |
|---|
| 149 | _channel.stop(); |
|---|
| 150 | } |
|---|
| 151 | if (!progressEvent) { |
|---|
| 152 | setState(PlayerState.BUFFERING); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } else if (state == PlayerState.BUFFERING && _sound.isBuffering == false && !_bufferFull) { |
|---|
| 156 | _bufferFull = true; |
|---|
| 157 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | if (!isNaN(bufferPercent) && !_bufferingComplete){ |
|---|
| 162 | if (bufferPercent == 100 && _bufferingComplete == false) { |
|---|
| 163 | _bufferingComplete = true; |
|---|
| 164 | } |
|---|
| 165 | sendBufferEvent(bufferPercent); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | if (_position < _item.duration) { |
|---|
| 169 | if (state == PlayerState.PLAYING){ |
|---|
| 170 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: _position, duration: _item.duration}); |
|---|
| 171 | } |
|---|
| 172 | } else if (_item.duration > 0) { |
|---|
| 173 | complete(); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | /** Seek in the _sound. **/ |
|---|
| 179 | override public function seek(pos:Number):void { |
|---|
| 180 | if (_sound && (pos < (_sound.bytesLoaded / _sound.bytesTotal) * item.duration) || item.start) { |
|---|
| 181 | clearInterval(_positionInterval); |
|---|
| 182 | _positionInterval = undefined; |
|---|
| 183 | if (_channel) { |
|---|
| 184 | _channel.stop(); |
|---|
| 185 | } |
|---|
| 186 | _position = pos; |
|---|
| 187 | play(); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | /** Destroy the _sound. **/ |
|---|
| 193 | override public function stop():void { |
|---|
| 194 | clearInterval(_positionInterval); |
|---|
| 195 | _positionInterval = undefined; |
|---|
| 196 | super.stop(); |
|---|
| 197 | if (_channel) { |
|---|
| 198 | _channel.stop(); |
|---|
| 199 | _channel = null; |
|---|
| 200 | } |
|---|
| 201 | try { |
|---|
| 202 | _sound.close(); |
|---|
| 203 | } catch (err:Error) { |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | /** Set the volume level. **/ |
|---|
| 209 | override public function setVolume(vol:Number):void { |
|---|
| 210 | _transformer.volume = vol / 100; |
|---|
| 211 | if (_channel) { |
|---|
| 212 | _channel.soundTransform = _transformer; |
|---|
| 213 | super.setVolume(vol); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|