Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在线程上用java播放音乐_Java_Multithreading_Media Player_Media - Fatal编程技术网

在线程上用java播放音乐

在线程上用java播放音乐,java,multithreading,media-player,media,Java,Multithreading,Media Player,Media,我目前正在创建虚拟鼓套件。Kinect正在录制我的动作,当我敲击虚拟鼓时,程序需要播放该鼓的声音。目前,我播放音乐的代码如下所示: void playInstrumentSound(InstrumentModel instrument) { if (instrument.getMedia() != null) { new Thread() { public void run() { instrument.setPla

我目前正在创建虚拟鼓套件。Kinect正在录制我的动作,当我敲击虚拟鼓时,程序需要播放该鼓的声音。目前,我播放音乐的代码如下所示:

void playInstrumentSound(InstrumentModel instrument) {
    if (instrument.getMedia() != null) {
        new Thread() {
            public void run() {
                instrument.setPlaying(true);
                MediaPlayer player = new MediaPlayer(instrument.getMedia());
                player.setVolume(1);
                player.play();
                try {
                    Thread.sleep(properties.getSleepLength());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                instrument.setPlaying(false);
                player.dispose();
            }
        }.start();
    }
}
InstrumentModel是我的类,它包含通过使用pathToSound初始化的媒体对象,pathToSound是资源文件夹中.wav文件的路径:

if (this.pathToSound != null) {
    media = new Media(new File(this.pathToSound).toURI().toString());
}
现在,这段代码没有达到我的预期,因为如果我敲了一个鼓,那么我就不能再敲它了,因为时间的属性.getSleepLength()值(现在大约是200ms)。如果我不睡觉,我就听不到完整的声音

例如,如果.wav文件的持续时间为300ms,并且我在playInstrumentSound()方法中使用Thread.sleep(300),那么我可以听到完整的声音,但在这300ms内不能播放不同的声音。但若我制作了Thread.sleep(50),那个么我几乎可以立即再次点击它,但我只听到50毫秒的.wav文件

我希望能够击鼓几乎立即,但也听到它的全部声音。我怎样才能做到?提前谢谢

编辑: 我刚刚有了一个想法来改变PlayMethod内部的顺序:

void playInstrumentSound(InstrumentModel instrument) {
    if (instrument.getMedia() != null) {
        instrument.setPlaying(true);
        MediaPlayer player = new MediaPlayer(instrument.getMedia());
        player.setVolume(1);
        player.play();
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                instrument.setPlaying(false);
            }
        }.start();
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                player.dispose();
            }
        }.start();
    }

现在,它应该在250毫秒后关闭声音,并在50毫秒后更改显示标志(如果显示标志为真,则不能再次击鼓)。您认为它可以工作吗?

您可以添加您的MediaPlayer类吗?MediaPlayer是来自javafx.scene.Media包的一个类。我在寻找一个可以用Java播放声音的库时找到了它。如果有更好的选择,请随时告诉我:)也许Java Sound API(我自己从未使用过,但有一个可下载的示例)看起来不错,我明天会试试,谢谢。