How-To's > How do I access volume controls?

Version: JavaFX 1.3

Use the volume and the mute instance variables of the MediaPlayer class to alter the volume level and to mute/unmute the audio stream. In general you should implement the following steps:

  1. Create a media player (see How do I add a video player).
  2. Create a control element as a Node extension.
    Tip: You can either construct this element by using the javafx.scene.shape and javafx.scene.control packages, use the prefabricated image added to the ImageView object, or create a more complicated implementation by extending the CustomNode class.
    volume control
  3. Bind the volume level and the mute status retrieved from the control element to the corresponding variables of the media player object.

The following code scheme illustrates this approach.

import javafx.scene.CustomNode;
import javafx.scene.media.MediaPlayer;
import javafx.scene.Scene;
import javafx.stage.Stage;

class VolumeControl extends CustomNode {
    public var volume: Number;
    public var mute: Boolean = false;
//Create your volume control here
}

class MyPlayer extends MediaPlayer {
    public override var volume on replace {  }
    public override var mute on replace { }
//Create your media player here
}


def myPlayer = MyPlayer{};
var volumeControl: VolumeControl;

Stage {
    scene: Scene {
        content: [
            //All the graphical components go here
            //Bidirectional binding is applied
            volumeControl = VolumeControl {
                volume: bind myPlayer.volume with inverse
                mute: bind myPlayer.mute with inverse
            }
        ]
    }
}

Examples

API Documentation

Last Updated: May 2010
[Return to How-To's Home]