From bb3e1db08c1d301a4354fb68f7dd87a49d554cae Mon Sep 17 00:00:00 2001 From: Anssi Piirainen Date: Mon, 10 Feb 2014 17:15:56 +0200 Subject: [PATCH] addnew top level config option 'persistVolumeLevel'. Does not store the volume level and create a 'flash cookie' if set to false. --- core/README.txt | 4 + .../org/flowplayer/config/Config.as | 5 + .../controller/MediaControllerFactory.as | 2 +- .../flowplayer/controller/VolumeController.as | 300 +++++++++--------- 4 files changed, 166 insertions(+), 145 deletions(-) diff --git a/core/README.txt b/core/README.txt index 62e9422..693228c 100644 --- a/core/README.txt +++ b/core/README.txt @@ -1,5 +1,9 @@ Version history: +3.2.19-dev +------ +- new top level config option 'persistVolumeLevel'. Does not store the volume level and create a 'flash cookie' if set to false. + 3.2.17 ------ - #75 set the child display list different when a gradient is set. diff --git a/core/src/actionscript/org/flowplayer/config/Config.as b/core/src/actionscript/org/flowplayer/config/Config.as index 70def5b..98a1998 100644 --- a/core/src/actionscript/org/flowplayer/config/Config.as +++ b/core/src/actionscript/org/flowplayer/config/Config.as @@ -258,5 +258,10 @@ package org.flowplayer.config { return _configObject; } + public function get persistVolumeLevel():Boolean { + if (! _configObject.hasOwnProperty("persistVolumeLevel")) return true; + return _configObject["persistVolumeLevel"]; + } + } } diff --git a/core/src/actionscript/org/flowplayer/controller/MediaControllerFactory.as b/core/src/actionscript/org/flowplayer/controller/MediaControllerFactory.as index 6ae0bad..b058b46 100644 --- a/core/src/actionscript/org/flowplayer/controller/MediaControllerFactory.as +++ b/core/src/actionscript/org/flowplayer/controller/MediaControllerFactory.as @@ -56,7 +56,7 @@ package org.flowplayer.controller { _providers = providers; _instance = this; _playerEventDispatcher = playerEventDispatcher; - _volumeController = new VolumeController(_playerEventDispatcher); + _volumeController = new VolumeController(_playerEventDispatcher, config.persistVolumeLevel); _config = config; _loader = loader; } diff --git a/core/src/actionscript/org/flowplayer/controller/VolumeController.as b/core/src/actionscript/org/flowplayer/controller/VolumeController.as index e2f3404..c70681a 100644 --- a/core/src/actionscript/org/flowplayer/controller/VolumeController.as +++ b/core/src/actionscript/org/flowplayer/controller/VolumeController.as @@ -17,150 +17,162 @@ */ package org.flowplayer.controller { - import org.flowplayer.flow_internal; - import org.flowplayer.model.PlayerEvent; - import org.flowplayer.util.Log; - import org.flowplayer.view.PlayerEventDispatcher; - - import flash.events.TimerEvent; - import flash.media.SoundChannel; - import flash.media.SoundTransform; - import flash.net.NetStream; - import flash.utils.Timer; - - use namespace flow_internal; - - /** - * @author api - */ - public class VolumeController { - - private var log:Log = new Log(this); - private var _soundTransform:SoundTransform; - private var _netStream:NetStream; - private var _storedVolume:VolumeStorage; - private var _storeDelayTimer:Timer; - private var _muted:Boolean; - private var _playerEventDispatcher:PlayerEventDispatcher; - private var _soundChannel:SoundChannel; - - public function VolumeController(playerEventDispatcher:PlayerEventDispatcher) { - _playerEventDispatcher = playerEventDispatcher; - _soundTransform = new SoundTransform(); - restoreVolume(); - _storeDelayTimer = new Timer(2000, 1); - _storeDelayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerDelayComplete); - } - - public function set netStream(netStream:NetStream):void { - _netStream = netStream; - setTransform(_muted ? new SoundTransform(0) : _soundTransform); - } - - private function setTransform(transform:SoundTransform):void { - if (_netStream) { - _netStream.soundTransform = transform; - } - if (_soundChannel) { - _soundChannel.soundTransform = transform; - } - } - - private function doMute(persistMuteSetting:Boolean):void { - log.debug("muting volume"); - if (dispatchBeforeEvent(PlayerEvent.mute())) { - _muted = true; - setTransform(new SoundTransform(0)); - dispatchEvent(PlayerEvent.mute()); - if (persistMuteSetting) - storeVolume(true); - } - } - - private function unMute():Number { - log.debug("unmuting volume to level " + _soundTransform.volume); - if (dispatchBeforeEvent(PlayerEvent.unMute())) { - _muted = false; - setTransform(_soundTransform); - dispatchEvent(PlayerEvent.unMute()); - storeVolume(false); - } - return volume; - } - - public function set volume(volumePercentage:Number):void { - if (this.volume == volumePercentage) return; - if (dispatchBeforeEvent(PlayerEvent.volume(volumePercentage))) { - if (volumePercentage > 100) { - volumePercentage = 100; - } - if (volumePercentage < 0) { - volume = 0; - } - _soundTransform.volume = volumePercentage / 100; - if (!_muted) { - setTransform(_soundTransform); - } - dispatchEvent(PlayerEvent.volume(this.volume)); - if (!_storeDelayTimer.running) { - log.info("starting delay timer"); + import org.flowplayer.flow_internal; + import org.flowplayer.model.PlayerEvent; + import org.flowplayer.util.Log; + import org.flowplayer.view.PlayerEventDispatcher; + + import flash.events.TimerEvent; + import flash.media.SoundChannel; + import flash.media.SoundTransform; + import flash.net.NetStream; + import flash.utils.Timer; + + use namespace flow_internal; + + /** + * @author api + */ + public class VolumeController { + + private var log:Log = new Log(this); + private var _soundTransform:SoundTransform; + private var _netStream:NetStream; + private var _storedVolume:VolumeStorage; + private var _storeDelayTimer:Timer; + private var _muted:Boolean; + private var _playerEventDispatcher:PlayerEventDispatcher; + private var _soundChannel:SoundChannel; + private var _persistVolumeLevel:Boolean; + + public function VolumeController(playerEventDispatcher:PlayerEventDispatcher, persistVolumeLevel:Boolean) { + _playerEventDispatcher = playerEventDispatcher; + _soundTransform = new SoundTransform(); + _persistVolumeLevel = persistVolumeLevel; + log.debug("will persist volume level? " + persistVolumeLevel); + + restoreVolume(); + _storeDelayTimer = new Timer(2000, 1); + _storeDelayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerDelayComplete); + } + + public function set netStream(netStream:NetStream):void { + _netStream = netStream; + setTransform(_muted ? new SoundTransform(0) : _soundTransform); + } + + private function setTransform(transform:SoundTransform):void { + if (_netStream) { + _netStream.soundTransform = transform; + } + if (_soundChannel) { + _soundChannel.soundTransform = transform; + } + } + + private function doMute(persistMuteSetting:Boolean):void { + log.debug("muting volume"); + if (dispatchBeforeEvent(PlayerEvent.mute())) { + _muted = true; + setTransform(new SoundTransform(0)); + dispatchEvent(PlayerEvent.mute()); + if (persistMuteSetting) + storeVolume(true); + } + } + + private function unMute():Number { + log.debug("unmuting volume to level " + _soundTransform.volume); + if (dispatchBeforeEvent(PlayerEvent.unMute())) { + _muted = false; + setTransform(_soundTransform); + dispatchEvent(PlayerEvent.unMute()); + storeVolume(false); + } + return volume; + } + + public function set volume(volumePercentage:Number):void { + if (this.volume == volumePercentage) return; + if (dispatchBeforeEvent(PlayerEvent.volume(volumePercentage))) { + if (volumePercentage > 100) { + volumePercentage = 100; + } + if (volumePercentage < 0) { + volume = 0; + } + _soundTransform.volume = volumePercentage / 100; + if (!_muted) { + setTransform(_soundTransform); + } + dispatchEvent(PlayerEvent.volume(this.volume)); + if (!_storeDelayTimer.running) { + log.info("starting delay timer"); _storeDelayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerDelayComplete); - _storeDelayTimer.start(); - } - } - } - - /** - * Gets the volume percentage. - */ - public function get volume():Number { - return _soundTransform.volume * 100; - } - - private function onTimerDelayComplete(event:TimerEvent):void { - storeVolume(); - } - - private function storeVolume(muted:Boolean = false):void { - log.info("persisting volume level"); - _storeDelayTimer.stop(); + _storeDelayTimer.start(); + } + } + } + + /** + * Gets the volume percentage. + */ + public function get volume():Number { + return _soundTransform.volume * 100; + } + + private function onTimerDelayComplete(event:TimerEvent):void { + storeVolume(); + } + + private function storeVolume(muted:Boolean = false):void { + _storeDelayTimer.stop(); _storeDelayTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerDelayComplete); - _storedVolume.volume = _soundTransform.volume; - _storedVolume.muted = muted; - _storedVolume.persist(); - } - - private function restoreVolume():void { - _storedVolume = LocalSOVolumeStorage.create(); - - _soundTransform.volume = _storedVolume.volume; - if (_storedVolume.muted) - doMute(false); - } - - private function dispatchBeforeEvent(event:PlayerEvent):Boolean { - return _playerEventDispatcher.dispatchBeforeEvent(event); - } - - private function dispatchEvent(event:PlayerEvent):void { - _playerEventDispatcher.dispatchEvent(event); - } - - public function get muted():Boolean { - return _muted; - } - - public function set muted(muted:Boolean):void { - if (muted) { - doMute(true); - } else { - unMute(); - } - } - - public function set soundChannel(channel:SoundChannel):void { - _soundChannel = channel; - setTransform(_muted ? new SoundTransform(0) : _soundTransform); - } - } + + if (_persistVolumeLevel) { + log.info("persisting volume level"); + _storedVolume.volume = _soundTransform.volume; + _storedVolume.muted = muted; + _storedVolume.persist(); + } + } + + private function restoreVolume():void { + if (!_persistVolumeLevel) { + _soundTransform.volume = 0.5; + return; + } + + _storedVolume = LocalSOVolumeStorage.create(); + + _soundTransform.volume = _storedVolume.volume; + if (_storedVolume.muted) + doMute(false); + } + + private function dispatchBeforeEvent(event:PlayerEvent):Boolean { + return _playerEventDispatcher.dispatchBeforeEvent(event); + } + + private function dispatchEvent(event:PlayerEvent):void { + _playerEventDispatcher.dispatchEvent(event); + } + + public function get muted():Boolean { + return _muted; + } + + public function set muted(muted:Boolean):void { + if (muted) { + doMute(true); + } else { + unMute(); + } + } + + public function set soundChannel(channel:SoundChannel):void { + _soundChannel = channel; + setTransform(_muted ? new SoundTransform(0) : _soundTransform); + } + } }