How-To's > How do I access volume controls?
Version: JavaFX 1.3Use 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:
- Create a media player (see How do I add a video player).
- Create a control element as a
Nodeextension.
Tip: You can either construct this element by using thejavafx.scene.shapeandjavafx.scene.controlpackages, use the prefabricated image added to theImageViewobject, or create a more complicated implementation by extending theCustomNodeclass.
- 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
- Dragging an MP3 Player Applet to the Desktop
This sample shows how to create a draggable player that users can save on the desktop for later use. - Playing Video on the Sides of a Rotating Cube
This sample shows how to play video on the sides of a rotating cube.
API Documentation
Last Updated: May 2010
[Return to How-To's Home]