【发布时间】:2021-05-23 09:40:05
【问题描述】:
我的应用使用一些简短的声音来提供用户反馈。我使用以下代码:
private void playSound(String fileName) {
try {
FileSystemStorage fss = FileSystemStorage.getInstance();
String sep = fss.getFileSystemSeparator() + "";
String soundDir; // sounds must be in a directory
if (fss.getAppHomePath().endsWith(sep)) {
soundDir = fss.getAppHomePath() + "sounds"; // device
} else {
soundDir = fss.getAppHomePath() + sep + "sounds"; // simulator/windows
}
if (!fss.exists(soundDir)) {
// first time a sound is played: create directory
fss.mkdir(soundDir);
}
String filePath = soundDir + sep + fileName;
if (!fss.exists(filePath)) {
// first time this sound is played: copy from resources (place file in <project>/src)
InputStream is = Display.getInstance().getResourceAsStream(getClass(), "/" + fileName);
OutputStream os = fss.openOutputStream(filePath);
com.codename1.io.Util.copy(is, os);
}
Media media = MediaManager.createMedia(filePath, false);
//media.setVolume(100);
media.play();
} catch (IOException ex) {
log("Error playing " + fileName + " " + ex.getMessage());
}
}
调用示例:
playSound("error.mp3");
这在设备和模拟器上运行良好。但是,如果我在模拟器(使用 Windows)中进行长时间的自动测试,大约每秒播放一次声音, 这会耗尽所有 RAM,直到 Windows 崩溃。但是,Windows 任务管理器没有显示 NetBeans 和 Java 进程的异常内存使用情况。
所以我的问题是:我的代码是否正确?这也可以在设备上发生吗?或者有没有办法在模拟器/Windows中防止这种情况发生?
附言
我还尝试了How to bundle sounds with Codename One? 的代码。那也有同样的问题 有些声音会丢失(不播放)。
我也尝试了Codename One - Play a sound 中的简单代码,但这不起作用。
【问题讨论】:
标签: audio memory-leaks codenameone