Creating a volume slider

How to

Create a volume slider in your application that allows a user to move the slider between a range of values.

Solution

Construct the volume slider variable in the root of your class

var myVSlider:VolumeSlider = new VolumeSlider();
// Placing this at the class root allows other 
// application elements to reference it

Initialize your slider's attributes

// Set location
myVSlider.x = 50;
myVSlider.y = 50;
// Set width
myVSlider.width = 250;

// Optional: Starting value
myVSlider.value = 33;
// Optional: Set minimum bound - default of 0
myVSlider.minimum = 1;
// Optional: Set maximum bound - default of 100
myVSlider.maximum = 100;

// Call event functions when slider is moved
myVSlider.addEventListener( SliderEvent.MOVE, sliderChange );
myVSlider.addEventListener( SliderEvent.START, sliderStart );
myVSlider.addEventListener( SliderEvent.END, sliderStop );

// Add slider to stage
this.addChild( myVSlider );

Set audioBoost limits (optional)

// Set max volume limit for when audioBoost is disabled
myVSlider.maxUnboostedAudioLevel = 67;
// Turn audioBoost on
myVSlider.audioBoostEnabled = true;

Create the functions in your class to be called by your slider's interaction events

private function sliderChange( event:SliderEvent ) : void {
	// Get and output value of slider when moving
	var newlevel:int = Math.round( event.target.value );
	trace( "slider value: ", newlevel );
}

private function sliderStart( event:SliderEvent ) : void {
	// Get and output value of slider's initial position
	var newlevel:int = Math.round( event.target.value );
	trace( "started moving at: ", newlevel );
}

private function sliderStop( event:SliderEvent ) : void {
	// Get and output value of slider's ending position
	var newlevel:int = Math.round( event.target.value );
	trace( "stopped moving at: ", newlevel );
}

Build Requirements

You must include the following classes in your project:

import qnx.fuse.ui.slider.VolumeSlider;
import qnx.fuse.ui.events.SliderEvent;

Discussion

When you create a volume slider, you declare the VolumeSlider variable and set its properties to whatever values your application requires. To add functionality, add SliderEvent listener functions to perform any actions that are defined in the function upon a MOVE, START, or END of slider interaction. You can also reference the value of your slider's current position anywhere in the class by using your slider's value property.

The audioBoost feature enables you to set a secondary maximum level for your slider in addition to the one defined by the basic maximum property. After setting this secondary limit with maxUnboostedAudioLevel, when the audioBoostEnabled property is set to true, your volume slider will only serve values between the minimum and the secondary limit. When set to false, your slider will show a blue bar when the value is below the unboosted limit, and turn red when positioned in the extra higher bounds.

This class does not automatically adjust the system volume, but instead simply creates a user interface through which you can obtain values and adjust your application's settings accordingly. For further information on audio output, please see the AudioManager API