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