package FlashChat { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundTransform; import flash.net.URLRequest; import flash.xml.XMLNode; public class Sounds extends EventDispatcher { public static const SUBMIT_MESSAGE: String = 'SubmitMessage'; public static const RECIEVE_MESSAGE: String = 'ReceiveMessage'; public static const ENTER_ROOM: String = 'EnterRoom'; public static const LEAVE_ROOM: String = 'LeaveRoom'; public static const INITIAL_LOGIN: String = 'InitialLogin'; public static const LOGOUT: String = 'Logout'; public static const PRIVATE_RECIEVED: String = 'PrivateMessageReceived'; public static const INVITATION_RECIEVED: String = 'InvitationReceived'; public static const COMBO_OPEN_CLOSE: String = 'ComboListOpenClose'; public static const USER_BANNED: String = 'UserBannedBooted'; public static const USER_MENU_OVER: String = 'UserMenuMouseOver'; public static const ROOM_OPEN_CLOSE: String = 'RoomOpenClose'; public static const POPUP_OPEN: String = 'PopupWindowOpen'; public static const POPUP_CLOSE: String = 'PopupWindowCloseMin'; public static const PRESS_BUTTON: String = 'PressButton'; public static const USER_ENTERS: String = 'OtherUserEnters'; public static const RING_BELL: String = 'RingBell'; public static var soundsTotal: uint = 0; public static var soundsLoaded: uint = 0; public static var isPlaying: Boolean = false; public static var filesLoaded: Boolean = false; public static var commonMute: Boolean = false; public static var commonTrans: * = null; public static var sounds: Array = [ {id: 'SubmitMessage', name : null, sound: null, trans: null, mute: false}, {id: 'ReceiveMessage', name : null, sound: null, trans: null, mute: false}, {id: 'EnterRoom', name : null, sound: null, trans: null, mute: false}, {id: 'LeaveRoom', name : null, sound: null, trans: null, mute: false}, {id: 'InitialLogin', name : null, sound: null, trans: null, mute: false}, {id: 'Logout', name : null, sound: null, trans: null, mute: false}, {id: 'PrivateMessageReceived', name : null, sound: null, trans: null, mute: false}, {id: 'InvitationReceived', name : null, sound: null, trans: null, mute: false}, {id: 'ComboListOpenClose', name : null, sound: null, trans: null, mute: false}, {id: 'UserBannedBooted', name : null, sound: null, trans: null, mute: false}, {id: 'UserMenuMouseOver', name : null, sound: null, trans: null, mute: false}, {id: 'RoomOpenClose', name : null, sound: null, trans: null, mute: false}, {id: 'PopupWindowOpen', name : null, sound: null, trans: null, mute: false}, {id: 'PopupWindowCloseMin', name : null, sound: null, trans: null, mute: false}, {id: 'PressButton', name : null, sound: null, trans: null, mute: false}, {id: 'OtherUserEnters', name : null, sound: null, trans: null, mute: false}, {id: 'RingBell', name : '', sound: null, trans: null, mute: false} ]; public function Sounds(): void { super(); } public static function initSounds(node: XMLNode, settings: XMLNode):void { soundsLoaded = 0; soundsTotal = 0; if (commonTrans == null) { commonTrans = new SoundTransform; commonTrans.volume = settings.attributes['volume']/100; commonTrans.pan = settings.attributes['pan']/100; commonMute = settings.attributes['muteAll']; } else { var tmpTrans: Object = commonTrans; commonTrans = new SoundTransform; commonTrans.volume = tmpTrans.volume; commonTrans.pan = tmpTrans.pan; } for (var soundName: String in node.attributes) { for (var soundObj: String in sounds) { if (sounds[soundObj].id == soundName) { soundsTotal++; sounds[soundObj].sound = new Sound(new URLRequest(Settings.path + node.attributes[soundName])); sounds[soundObj].sound.addEventListener(IOErrorEvent.IO_ERROR, function(): void { trace(node.attributes[soundName]); soundsLoaded++; Settings.dispatchEvent(new Event('soundLoaded')); }); sounds[soundObj].sound.addEventListener(Event.COMPLETE, function():void { soundsLoaded++; Settings.dispatchEvent(new Event('soundLoaded')); }); if (sounds[soundObj].trans == null) { sounds[soundObj].trans = new SoundTransform(1, 0); sounds[soundObj].mute = (settings.attributes['mute'+soundName] == '1'); } } } } filesLoaded = true; } public static function setTrans(id: String, vol: Number, pan: Number, mute: Boolean):void { for (var itemName:String in sounds) { if (sounds[itemName].id == id) { (sounds[itemName].trans as SoundTransform).volume = vol; (sounds[itemName].trans as SoundTransform).pan = pan; sounds[itemName].mute = mute; } } } public static function setCommonTrans(vol: Number, pan: Number, mute: Boolean):void { commonTrans.volume = vol; commonTrans.pan = pan; commonMute = mute; } public static function play(id: String):void { if (!commonMute && filesLoaded && !isPlaying) { for (var itemName:String in sounds) { if (sounds[itemName].id == id && !sounds[itemName].mute) { var curTrans: SoundTransform = new SoundTransform( sounds[itemName].trans.volume * commonTrans.volume, sounds[itemName].trans.pan + commonTrans.pan ); var sndChannel: SoundChannel = (sounds[itemName].sound as Sound).play(0, 1, curTrans); isPlaying = true; sndChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); function soundCompleteHandler(e: Event): void { isPlaying = false; sndChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); } break; } } } } } }