JavaFX AudioClip.play() [英] JavaFX AudioClip.play()

查看:417
本文介绍了JavaFX AudioClip.play()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个JavaFX应用程序,该应用程序使用声音作为按钮悬停的示例.我创建一个JavaFX AudioClip来创建和播放声音.到目前为止,一切正常(意思是:我听到了声音).

I am creating a JavaFX application that uses sounds for example for Button hovering. I create an JavaFX AudioClip to create and play the sound. It works ok so far (meaning: I hear the sound).

当play();方法被调用,声音立即播放.如果将鼠标悬停10次,我会听到10次声音. 但是:在后台,当play()方法返回时,JavaFX正在创建数百个线程(每个调用数百个).我什至看不到它到底是什么,因为有很多东西,Eclipse甚至无法正确显示它们(只有一个白色区域和一个疯狂的上下滚动滚动条).

When the play(); method is called, the sound is played immediatly. If I hover the button 10 times, I hear the sound 10 times. BUT: in the background JavaFX is creating hundreds of threads when the play() method returns (several hundred for each call). I cannot even see what it actually is, because there are so many, Eclipse does not even show them properly (there is just a white area and a crazy jumping scrollbar up and down).

这会导致巨大的滞后,我不明白JavaFX在这里做什么!声音总是一样的,所以我确实已经将它缓存到一个哈希图中,但是问题不在于实例化AudioClip,当play()方法返回时,它显然正在堆积.

This causes a massive lag and I do not understand what JavaFX is doing here! It is always the same sound, so I do have cached it into a hashmap already, but the problem is not instantiating the AudioClip, it is clearly stacking up when the play() method returns.

我已经研究了好几个小时,但是我想不出一种减少滞后的解决方法(除了可能我减小了声音文件的大小之外).

I have been looking into this for hours, but I can't figure out a workaround to reduce the lag (other than maybe reduce the size of the soundfiles, which I did).

                final AudioClip soundClip;
                if (audioClipCache.get(url.toString()) != null) {
                    // Log.info("Playing sound from cache...");
                    soundClip = audioClipCache.get(url.toString());
                } else {
                    // Log.info("Caching sound...");
                    soundClip = new AudioClip(url.toString());
                    audioClipCache.put(url.toString(), soundClip);
                }

                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        soundClip.play(soundVolume);
                    }
                });

暂时忘记哈希表以缓存AudioClips,这没有任何区别.所谓的以下代码(例如,连续10次)使Java疯狂大约10秒钟.

Forget the hashmap to cache the AudioClips for a moment, that does not make any difference whatsoever. So called the following code, say, 10 times in a row, leads Java to go crazy for about 10 seconds.

                AudioClip soundClip = new AudioClip(url.toString());
                soundClip.play(soundVolume);

这可以正常工作(例如在互联网上的5.000.000个示例中),但是在play()之后,它会像疯了一样产生线程(和滞后);方法被调用(每个悬停/调用(按所描述的方式)有数百个线程).

That works as it should (as in 5.000.000 examples across the internet), but it produces Threads (and Lag) like crazy after the play(); method is called (several hundred threads per hover / call (as descibed)).

推荐答案

我认为您在代码的其他地方遇到了问题.我已经将您的描述简化为以下示例(很简单):

I think you have a problem elsewhere in your code. I've distilled your description down to this (admittedly simplistic) example:

@Override
public void start(Stage arg0) throws Exception
{
    Thread.sleep(10000);

    AudioClip audioClip = new AudioClip(Paths.get("src/main/resources/stackoverflow/audio/alert.wav").toUri().toString());

    for (int i = 0; i < 100; i++)
    {
        int volume = i;
        Platform.runLater(() -> audioClip.play(volume));
        Thread.sleep(10);
    }

    arg0.show();
}

看着Java Mission Control十秒钟,我看到一次创建了100个线程.但是,它们都立即死亡(它们已经播放了简短的警报声音并完成了操作).因此,实时线程"图最多增加了100个线程,然后又下降到几秒钟之内.

Watching Java Mission Control at the ten second mark, I see 100 threads get created all at once. However, they all immediately die (they've played the short alert sound and are done). So the "Live Thread" graph spikes up by 100 threads and then drops right back to where it was within a couple of seconds.

您的代码中是否还有其他地方正在占用资源或线程引用?这是尝试找出为什么要繁殖的最好的建议.

Is there something elsewhere in your code that is holding onto a resource or a thread reference? That's my best suggestion for trying to find out why you're multiplying.

这篇关于JavaFX AudioClip.play()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆