Skip to content

Commit

Permalink
Merge pull request #59 from DecryptingElectrons/bugfix_operate_with_u…
Browse files Browse the repository at this point in the history
…navailable_sound

created fix to disable sound when unavailable
  • Loading branch information
xspanger3770 authored Aug 22, 2023
2 parents 85979f0 + 1df58c8 commit 6f58755
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public synchronized void handleException(Throwable e) {
}
}

public synchronized void handleWarning(Throwable e) {
System.err.println((e instanceof RuntimeApplicationException) + ", " + e.getCause());
showWarning(e.getMessage());
}

private void showWarning(String message) {
JOptionPane.showMessageDialog(parent, message, "Warning", JOptionPane.WARNING_MESSAGE);
}

private void showDetailedError(Throwable e) {
errorCount++;
if (errorCount == 2) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/globalquake/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import globalquake.database.StationDatabaseManager;
import globalquake.database.StationSource;
import globalquake.exception.ApplicationErrorHandler;
import globalquake.exception.RuntimeApplicationException;
import globalquake.exception.FatalIOException;
import globalquake.geo.taup.TauPTravelTimeCalculator;
import globalquake.regions.Regions;
Expand Down Expand Up @@ -68,7 +69,13 @@ private static void initAll() throws Exception {
Scale.load();
databaseMonitorFrame.getMainProgressBar().setString("Loading sounds...");
databaseMonitorFrame.getMainProgressBar().setValue((int) (2 / 6.0 * 100.0));
Sounds.load();
try{
//Sound may fail to load for a variety of reasons. If it does, this method disables sound.
Sounds.load();
} catch (Exception e){
RuntimeApplicationException error = new RuntimeApplicationException("Failed to load sounds. Sound will be disabled", e);
getErrorHandler().handleWarning(error);
}
databaseMonitorFrame.getMainProgressBar().setString("Loading travel table...");
databaseMonitorFrame.getMainProgressBar().setValue((int) (3 / 6.0 * 100.0));
TauPTravelTimeCalculator.init();
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/globalquake/sounds/Sounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import globalquake.exception.FatalIOException;
import globalquake.ui.settings.Settings;
import org.tinylog.Logger;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
Expand All @@ -27,6 +28,7 @@ public class Sounds {
public static final Clip[] countdowns = new Clip[countdown_levels.length];

public static final boolean soundsEnabled = true;
public static boolean soundsAvailable = true;

private static final String[] shindoNames = { "0", "1", "2", "3", "4", "5minus", "5plus", "6minus", "6plus", "7" };

Expand All @@ -37,7 +39,8 @@ private static Clip loadSound(String res) throws FatalIOException {
clip.open(audioIn);
return clip;
} catch(Exception e){
throw new FatalIOException("Cannot load sound: "+res, e);
soundsAvailable = false;
throw new FatalIOException("Failed to load sound: "+res, e);
}
}

Expand Down Expand Up @@ -75,7 +78,8 @@ public static Clip nextLevelBeginsWith1(int i) {
}

public static void playSound(Clip clip) {
if(!Settings.enableSound) {
if(!Settings.enableSound || !soundsAvailable) {
Logger.debug(clip.toString() + " not played. Sound disabled.");
return;
}
if (soundsEnabled && clip != null) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/globalquake/ui/globalquake/GlobalQuakePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import globalquake.geo.GeoUtils;
import globalquake.geo.Level;
import globalquake.geo.Shindo;
import globalquake.sounds.Sounds;
import globalquake.ui.StationMonitor;
import globalquake.ui.globalquake.feature.FeatureArchivedEarthquake;
import globalquake.ui.globalquake.feature.FeatureEarthquake;
Expand Down Expand Up @@ -110,7 +111,16 @@ private void drawTexts(Graphics2D g) {
g.drawString(str, getWidth() - g.getFontMetrics().stringWidth(str) - 6, getHeight() - 9);

List<String> settingsStrings = new ArrayList<>();
settingsStrings.add("Sound Alarms: %s (S)".formatted(Settings.enableSound ? "Enabled" : "Disabled"));

//If sound is not available, set a special message
if(!Sounds.soundsAvailable)
{
settingsStrings.add("Sound Alarms unavailable");
}
else{
settingsStrings.add("Sound Alarms: %s (S)".formatted(Settings.enableSound ? "Enabled" : "Disabled"));
}

settingsStrings.add("Earthquakes: %s (E)".formatted(Settings.displayArchivedQuakes ? "Enabled" : "Disabled"));
int _y = getHeight() - 6;
g.setColor(Color.MAGENTA);
Expand Down

0 comments on commit 6f58755

Please sign in to comment.