Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #205 from flowplayer/feature/persist-volume
Browse files Browse the repository at this point in the history
addnew top level config option 'persistVolumeLevel'. Does not store the ...
  • Loading branch information
anssip committed Feb 10, 2014
2 parents aadcdc7 + bb3e1db commit 35b1ca0
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 145 deletions.
4 changes: 4 additions & 0 deletions core/README.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions core/src/actionscript/org/flowplayer/config/Config.as
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,10 @@ package org.flowplayer.config {
return _configObject;
}

public function get persistVolumeLevel():Boolean {
if (! _configObject.hasOwnProperty("persistVolumeLevel")) return true;
return _configObject["persistVolumeLevel"];
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
300 changes: 156 additions & 144 deletions core/src/actionscript/org/flowplayer/controller/VolumeController.as
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 35b1ca0

Please sign in to comment.