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