I am trying to play background audio in a loop in a JavaFX 2.0 application using the JavaFX SDK 2.0.1. I decided to use MediaPlayer created by the following code snippet:
MediaPlayerBuilder .create().media(BACKGROUND_MEDIA) .cycleCount(MediaPlayer.INDEFINITE);
It basically works, but when a new cycle begins, there is a tiny (latent?) Gap between the end and the beginning of the sound. So this is not a working option for me, since it does not play a clean loop.
I decided to create a new MediaPlayer object and start playing it every time Media ends. So far this is wonderful. In addition, I use a button that plays a short AudioClip when pressed. I found that frequent and quick clicks of this button lead to interruptions in the background sound. I created an example to reproduce this behavior by playing non-player AudioClip with volume 0 when the button is pressed once. The example is not self-sufficient, since the required audio files are missing. To place 2 audio files in the source directory of the project requires:
- click.wav (really short click sound ~ 300 ms)
- background.wav (~ 5 seconds audio)
How do I get a clear sound path to play in the background without these interruptions when other sounds are played with one shot? Is this just a performance issue?
Example:
package mediatest; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.media.AudioClip; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaPlayerBuilder; import javafx.stage.Stage; public class MediaTest extends Application { private static final AudioClip CLICK_AUDIOCLIP = new AudioClip(MediaTest.class.getResource("/click.wav").toString()); private static final Media BACKGROUND_MEDIA = new Media(MediaTest.class.getResource("/background.wav").toString()); private MediaPlayerBuilder builder; public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); this.builder = MediaPlayerBuilder .create() .media(BACKGROUND_MEDIA) .onEndOfMedia(new Runnable() { public void run() { MediaPlayer player = MediaTest.this.builder.build(); player.play(); } }); MediaPlayer player = this.builder.build(); player.play(); Button btn = new Button(); btn.setText("Repeat playing short audio clip"); btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) {
pmoule
source share