Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+13 votes

I have a loopable WAV file of a rain storm, what would be the best way to play it on a loop while in a Scene?

asked in CSC 305 Fall 2024 by (1.2k points)

2 Answers

+6 votes

Based on this article on Stack Overflow, it looks like you could do this by opening a DataLine and looping a Clip of the sound. I don't know how you're generating your other sounds, though, so maybe you were trying to avoid having to deal with writing to or opening/closing the line. It should at least work on its own this way.
https://stackoverflow.com/questions/77719334/how-do-you-make-an-audioclip-mediaplayer-in-javafx-loop-perfectly

answered by (4.5k points)
+5 votes

To play a looping audio file in a JavaFX Scene, load the audio using AudioClip, set it to loop indefinitely with setCycleCount, and start playback with play when the Scene loads. To stop the audio when the window closes and prevent it from continuing in the background, use setOnCloseRequest with stop.

answered by (4.4k points)
+1

InteliJ warns me when I use AudioClip that it's deprecated. So while that would probably work, I'm not sure it's the best way.

...