How to play clear sound loops and one-time sounds in parallel in JavaFX 2.0?

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) { //Simulation of many button clicks MediaTest.CLICK_AUDIOCLIP.setCycleCount(AudioClip.INDEFINITE); MediaTest.CLICK_AUDIOCLIP.play(0); } }); root.getChildren().add(btn); primaryStage.setScene(scene); primaryStage.show(); } } 
+7
source share
2 answers

Have you looked at the ExecutorService ? Then you will have a number of predefined threads:

 ExecutorService service = Executors.newFixedThreadPool(4); 

where 4 is the number of threads it makes. This will improve performance because it uses the threads that are already created, rather than creating a new one every time you want to start something.

You would create a Runnable and execute it with the service as follows:

 Runnable r = new Runnable() { @Override public void run() { playSound(); } }; service.execute(r); 

This will not only improve performance, but will also automatically assign the task to a thread that is not currently busy in the thread pool.

Also look at this: Playing sound circuits using javafx , which I believe will solve your problem with low latency.

EDIT: hell, I didn't know this post was that old. This was the best google result.

+1
source

Using a different thread in

 public void handle(ActionEvent event) { Platform.runLater(new Runnable() { @Override public void run() { //Simulation of many button clicks MediaTest.CLICK_AUDIOCLIP.setCycleCount(AudioClip.INDEFINITE); MediaTest.CLICK_AUDIOCLIP.play(0); } }); } }); 

May solve interrupt problem

0
source

All Articles