-1

I'm having some issues playing mp3 files using AudioClip with JavaFX.

my code is like this:

// define String path to mp3 file
myFilePath = "path/to/file.mp3";

// Create an AudioClip Object from path
audioToPlay = new AudioClip(Objects.requireNonNull(getClass().getResource(audioToPlay)).toExternalForm());

// play the AudioClip
audioToPlay.play();

The audio files I am playing are all mp3 files and they are all fairly short (< 5 seconds). About 80-90% of the files are playing without any issue but the rest aren't playing at all, or are only playing for a very short time (less than a second so it sounds like a click).

I have no idea why the 10-20% aren't working; they play perfectly fine using my media player and from my browser.

Is the issue because I'm using AudioClip? Should I be using something else to play my short audio files?

Edit: myFilePath is defined as a String and audioToPlay is defined as an AudioClip object.

TLT-Dave
  • 14
  • 1
  • 3

1 Answers1

0

AudioClip is appropriate for short files that can be held in memory. This issue might be related to how or when you are calling the play() method. Does this occur when calling the play method via a button click, where the GUI stays open? Some folks don't realize playback is a daemon thread. Act of playback won't prevent a program from closing if the program triggers the sound and then completes and exits before the sound finishes playing. Also FWIW, once loaded into memory, there's no need to keep reloading AudioClip over and over.

Thought I'd add this: it seems to me that sometimes Java doesn't gracefully handle the first audio call in a session, that there is often a stutter. Due to this, I often play a "silent" sound at the start of a program, in order to get things going. Just mentioning, in case the problem occurs when the program is first getting going. If you use a GUI (with button to make sound) then you can test if an additional call repeats the problem or not, and thus help verify whether it's the sound file or the circumstance.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • Yes, the play() is called after a GUI button press. You'll have to forgive me but I don't know what a daemon thread is as I've not really explored threading yet. Do you mean I need to play the audio in a separate thread? – TLT-Dave May 16 '21 at 20:24
  • AudioClip will launch a thread, so there is no need to do so. It launches a daemon thread. Here is an explanation of daemon threads. https://www.geeksforgeeks.org/difference-between-daemon-threads-and-user-threads-in-java/ Do try making the AudioClip an instance variable and ONLY play it, do not load it from the button. That might help. If not, my next step would be to see if there is a way to post the sound file so we can take a look at it. – Phil Freihofner May 16 '21 at 20:29
  • Thank you for your reply. The AudioClip is an instance variable. I'm trying to make it dynamically load different mp3 files (which is does). As I mentioned, the majority of mp3 files play correctly. It's just a portion of them which seem to only play microseconds of sound? I'm not sure how to share one of the problem mp3 files. – TLT-Dave May 16 '21 at 21:04
  • @TLT-Dave What you describe suggests the issue is with the MP3 file(s). I wouldn't be surprised if the implementation used by JavaFX is more strict than other applications (such as your media player). Try using some tool that can check the problem MP3 files for errors. – Slaw May 16 '21 at 21:31
  • TLT-Dave, post the audio asset on Github perhaps? @Slaw, any suggestions on tools to try? I've only used Audacity and my IDE (Sonar) when mp3's involved. I still think making the loading and playing separate operations be helpful. If you can't load a file in advance and hold it in memory, should probably use a MediaPlayer for less latency. (AudioClip requires complete load and decompress before commencing playing, MediaPlayer only loads and decompresses a buffer's worth before starting. BTW: does the mp3 file work with MediaPlayer? – Phil Freihofner May 16 '21 at 21:47
  • 1
    Personally, I've never had much cause to diagnose MP3 files so I don't have any particular suggestions. [This Google search](https://www.google.com/search?q=mp3+check+for+errors) gives some options. And I agree, the `AudioClip` should not be created _every_ time it needs to be played (better to cache). – Slaw May 16 '21 at 22:07