简体   繁体   English

JavaFX AudioClip 无法播放某些 mp3 文件

[英]JavaFX AudioClip doesn't play certain mp3 files

I'm having some issues playing mp3 files using AudioClip with JavaFX.我在使用带有 JavaFX 的 AudioClip 播放 mp3 文件时遇到一些问题。

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).我正在播放的音频文件都是 mp3 文件,而且都很短(< 5 秒)。 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).大约 80-90% 的文件可以正常播放,但 rest 根本不播放,或者只播放了很短的时间(不到一秒,所以听起来像是咔哒声)。

I have no idea why the 10-20% aren't working;我不知道为什么 10-20% 不起作用; they play perfectly fine using my media player and from my browser.他们使用我的媒体播放器和我的浏览器播放得非常好。

Is the issue because I'm using AudioClip?问题是因为我使用的是 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.编辑: myFilePath 被定义为字符串,audioToPlay 被定义为 AudioClip object。

AudioClip is appropriate for short files that can be held in memory. AudioClip适用于可以保存在 memory 中的短文件。 This issue might be related to how or when you are calling the play() method.此问题可能与您调用 play() 方法的方式或时间有关。 Does this occur when calling the play method via a button click, where the GUI stays open?通过单击按钮调用播放方法时是否会发生这种情况,GUI 保持打开状态? 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.同样 FWIW,一旦加载到 memory 中,就无需一遍又一遍地重新加载AudioClip

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.想我会补充一点:在我看来,有时 Java 不能优雅地处理 session 中的第一个音频呼叫,通常会出现口吃。 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.如果您使用 GUI(带有按钮来发出声音),那么您可以测试其他呼叫是否重复问题,从而帮助验证它是声音文件还是环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM