java中使用剪辑和尝试 - 与 - 资源块,没有声音结果 [英] Java Use a Clip and a Try - with - resources block which results with no sound

查看:205
本文介绍了java中使用剪辑和尝试 - 与 - 资源块,没有声音结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写我的AudioManager类为我校的项目,我遇到了一个问题。我的教授告诉我加载我与尝试,与资源块,而不是使用的try / catch(见code以下)的所有资​​源。我使用javax.sound.sampled.Clip中从剪辑类和一切与我PlaySound(字符串路径)方法使用try / catch语句完美的作品/如果我不关闭()的剪辑。我知道,如果我关闭()剪辑,我不能再使用它。我已阅读Oracle文档进行剪辑和尝试,具有资源,但我无法找到一个解决方案。所以,我想知道的是:

I am rewriting my AudioManager class for my school project and I encountered a problem. My professor told me to load all my resources with the Try-with-resources block instead of using try/catch ( See code below ). I am using the Clip class from javax.sound.sampled.Clip and everything works perfectly with my PlaySound(String path) method that uses try/catch/ if I don't close() the Clip. I know that if I close() the Clip I can't use it anymore. I have read the Oracle Docs for Clip and Try-with-resources but I could not find a solution. So What I would like to know is:

是否可以使用Try-与资源块播放/从剪辑听到声音关闭之前?

Is it possible to use the Try-with-resource block to play/hear the sound from the clip before it closes?

// Uses Try- with resources. This does not work.
public static void playSound(String path) {
try {
    URL url = AudioTestManager.class.getResource(path);
    try (Clip clip = AudioSystem.getClip()){
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    clip.open(ais);
    clip.start();
    }
} catch( LineUnavailableException | UnsupportedAudioFileException  | IOException  e) {
    e.printStackTrace();}
}

// Does not use Try- with resources. This works.

public static void playSound2(String path) {
Clip clip = null;
try {
    URL url = AudioTestManager.class.getResource(path);
    clip = AudioSystem.getClip();
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    clip.open(ais);
    clip.start();

}
catch( LineUnavailableException | UnsupportedAudioFileException  | IOException  e) {
    e.printStackTrace();}
finally {
   // if (clip != null) clip.close();
}
}

在此先感谢!

推荐答案

的问题是,尝试-与资源块将自动关闭它夹创建块时结束导致播放停止。

The problem is that try-with-resources block will automatically close the Clip created in it when the block finishes causing the playback to stop.

在你的其他的例子,因为你不手动关闭它,回放可以继续。

In your other example since you don't close it manually, the playback can continue.

如果你想关闭剪辑当演奏完毕,你可以将它添加一个 LineListener 通过 addLineListener()并关闭它​​,当您收到停止事件是这样的:

If you want to close the Clip when it finished playing, you can add a LineListener to it with the addLineListener() and close it when you receive a STOP event like this:

final Clip clip = AudioSystem.getClip();
// Configure clip: clip.open();
clip.start();

clip.addLineListener(new LineListener() {
    @Override
    public void update(LineEvent event) {
        if (event.getType() == LineEvent.Type.STOP)
            clip.close();
    }
});

这篇关于java中使用剪辑和尝试 - 与 - 资源块,没有声音结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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