use getResourceAsStream instead of getResource, for #1617

master
melvinzhang 2018-11-01 12:53:45 +08:00
parent 470c6fe675
commit 5cb0594ee2
2 changed files with 9 additions and 8 deletions

View File

@ -45,10 +45,10 @@ public enum MagicSound {
}
};
private final URL soundUrl;
private final String soundFile;
private MagicSound(final String aFilename) {
this.soundUrl = MagicResources.getSoundUrl(aFilename);
this.soundFile = aFilename;
}
private boolean isUISound() {
@ -80,7 +80,7 @@ public enum MagicSound {
*/
public void play(int volPercent) {
if (volPercent > 0 && volPercent <= 100) {
executor.execute(() -> playSound(soundUrl, volPercent));
executor.execute(() -> playSound(soundFile, volPercent));
}
}
@ -117,11 +117,11 @@ public enum MagicSound {
clip.start();
}
private static void playSound(URL url, int volPercent) {
try (final AudioInputStream ins = AudioSystem.getAudioInputStream(url)) {
private static void playSound(final String soundFile, final int volPercent) {
try (final AudioInputStream ins = AudioSystem.getAudioInputStream(MagicResources.getSoundStream(soundFile))) {
playClip(ins, volPercent);
} catch (Exception ex) {
System.err.println("WARNING. Unable to play clip " + url.toExternalForm() + ", " + ex.getMessage());
System.err.println("WARNING. Unable to play clip " + soundFile + ", " + ex.getMessage());
// turn off all sound permanently.
GeneralConfig.set(IntegerSetting.GAME_VOLUME, 0);
GeneralConfig.set(IntegerSetting.UI_VOLUME, 0);

View File

@ -4,6 +4,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import magic.data.MagicIcon;
@ -49,8 +50,8 @@ public final class MagicResources {
return instance.getClass().getResourceAsStream("/magic/data/textures/" + imageFilename);
}
public static URL getSoundUrl(final String filename) {
return instance.getClass().getResource("/soundfx/" + filename);
public static BufferedInputStream getSoundStream(final String filename) {
return new BufferedInputStream(instance.getClass().getResourceAsStream("/soundfx/" + filename));
}
public static InputStreamReader getH2ScriptFile(String filename) {