Java剪辑(声音/音频)内存关闭后关闭() [英] Java Clip (Sound / Audio) Memory Leak after closing with close()
本文介绍了Java剪辑(声音/音频)内存关闭后关闭()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码创建一个新的音频片段,播放它,睡眠3秒钟,然后在播放完毕后关闭它。尽管调用了close(),但每次while循环运行时,我都会看到jvm的内存使用量会增加声音片段的大小。
The following code creates a new audio clip, plays it, sleeps for 3 seconds and then closes it when it is finished playing. Despite the call to close(), I am watching the memory usage of the jvm go up by the size of the sound clip every time the while loop is run.
I参与用java编码的游戏,我正在处理声音。每次播放声音时,我都无法使用增加的内存。
I'm participating in a game coded in java, and am handling the sound. I cannot have the memory i'm using increase everytime a sound is played.
我缺少什么?
import java.io.File;
import javax.sound.sampled.*;
public class ClipLeak{
public static void main(String[] args) throws Exception{
while(true){
File soundFile = new File("./sound.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
sound.close();
clip.addLineListener(new LineListener(){
public void update(LineEvent event){
if(event.getType() == LineEvent.Type.STOP){
event.getLine().close();
}
}
});
clip.start();
Thread.sleep(2000);
}
}
}
推荐答案
Try this
import java.io.File;
import javax.sound.sampled.*;
public class ClipLeak{
public static void main(String[] args) throws Exception{
while(true){
File soundFile = new File("./sound.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
clip.addLineListener(new LineListener(){
public void update(LineEvent event){
if(event.getType() == LineEvent.Type.STOP){
event.getLine().close();
}
}
});
clip.start();
Thread.sleep(2000);
if (clip.isOpen()) {
clip.close();
sound.close();
}
}
}
}
这篇关于Java剪辑(声音/音频)内存关闭后关闭()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文