Java Reference
In-Depth Information
Audio and the JVM
As mentioned earlier, the JavaFX media API will not work for our purposes because it does not provide
access to the raw audio data as it is being played. The JavaFX API focuses on simple playback, which I am
sure provides all of the functionality most people require. It is worth taking a look at the JavaFX media
API anyway, because it becomes useful in other cases and will provide context for what we will be
implementing later in the chapter.
There are other ways to work with media and audio, in particular with Java. We will take a look at the
Java Sound API, which we will use to implement our audio visualizations.
Audio and JavaFX
JavaFX comes with classes that allow the playing of several media types including audio files. The
following are the core classes:
javafx.scene.media.AudioTrack
javafx.scene.media.Media
javafx.scene.media.MediaError
javafx.scene.media.Media.Metadata
javafx.scene.media.MediaPlayer
javafx.scene.media.MediaTimer
javafx.scene.media.MediaView
javafx.scene.media.SubtitleTrack
javafx.scene.media.Track
javafx.scene.media.TrackType
javafx.scene.media.VideoTrack
As we can see, JavaFX provides us with a simple set of classes for playing back video and audio.
Using these classes, loading and playing media in a JavaFX application is straightforward. Listing 9-1
shows a simple example of doing this.
Listing 9-1. JavaFXMediaExample.fx
function run():Void{
var media = Media{
source: "file:///Users/lucasjordan/astroidE_32_0001_0031.avi"
}
var mediaPlayer = MediaPlayer{
media: media;
}
var mediaView = MediaView{
mediaPlayer: mediaPlayer;
}
Stage {
title: "Chapter 9 - JavaFX Media Support"
width: 640
height: 480
scene: Scene{
Search WWH ::




Custom Search