Looping a sound with either javafx.scene.media.MediaPlayer or javafx.scene.media.AudioClip results in a clicking artefact making JFX audio unusable for things like background sounds in games or music.
Two reproducible test cases attached. The file contains a test signal (perfect sine wave at 1 kHz) and loops perfectly.
import javafx.application.Application;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;
/**
* Simple audio loop
*/
public class AudioClipLoopTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// download at https://www.dropbox.com/s/yagu1vsboqh0fv5/sinewave1s.wav
final AudioClip clip = new AudioClip("file:" + System.getProperty("user.home") + "/tmp/sinewave1s.wav");
clip.setCycleCount(AudioClip.INDEFINITE);
clip.play();
}
public static void main(String[] args){
launch(args);
}
}
The same with MediaPlayer:
import javafx.application.Application;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
/**
* Simple audio loop
*/
public class MediaPlayerAudioLoopTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// download at https://www.dropbox.com/s/yagu1vsboqh0fv5/sinewave1s.wav
final Media media = new Media("file:" + System.getProperty("user.home") + "/tmp/sinewave1s.wav");
final MediaPlayer player = new MediaPlayer(media);
player.setCycleCount(AudioClip.INDEFINITE);
player.setOnReady(()->player.play());
}
public static void main(String[] args){
launch(args);
}
}
In addition to that the MediaPlayer stops looping after 6 cycles, maybe related toRT-40409.
I have not found a workaround, so if I do not use native frameworks directly or use a framework like libgdx, I cannot continue with my application.
Two reproducible test cases attached. The file contains a test signal (perfect sine wave at 1 kHz) and loops perfectly.
import javafx.application.Application;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;
/**
* Simple audio loop
*/
public class AudioClipLoopTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// download at https://www.dropbox.com/s/yagu1vsboqh0fv5/sinewave1s.wav
final AudioClip clip = new AudioClip("file:" + System.getProperty("user.home") + "/tmp/sinewave1s.wav");
clip.setCycleCount(AudioClip.INDEFINITE);
clip.play();
}
public static void main(String[] args){
launch(args);
}
}
The same with MediaPlayer:
import javafx.application.Application;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
/**
* Simple audio loop
*/
public class MediaPlayerAudioLoopTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// download at https://www.dropbox.com/s/yagu1vsboqh0fv5/sinewave1s.wav
final Media media = new Media("file:" + System.getProperty("user.home") + "/tmp/sinewave1s.wav");
final MediaPlayer player = new MediaPlayer(media);
player.setCycleCount(AudioClip.INDEFINITE);
player.setOnReady(()->player.play());
}
public static void main(String[] args){
launch(args);
}
}
In addition to that the MediaPlayer stops looping after 6 cycles, maybe related to
I have not found a workaround, so if I do not use native frameworks directly or use a framework like libgdx, I cannot continue with my application.
- duplicates
-
JDK-8090414 AudioClip needs native implementation
- Open
-
JDK-8090414 AudioClip needs native implementation
- Open