diff --git a/src/main/java/musicplayer/ConfigManager.java b/src/main/java/musicplayer/ConfigManager.java index 25e2d6a..2bda40d 100644 --- a/src/main/java/musicplayer/ConfigManager.java +++ b/src/main/java/musicplayer/ConfigManager.java @@ -19,6 +19,7 @@ public final class ConfigManager { private static final String foldersKey = "libraryFolders"; private static final String databaseKey = "databaseDir"; private static final String libraryDisplayKey = "libraryDisplayKey"; + private static final String lastVolumeKey = "volume"; /** * @return List of directories used for music library indexing. @@ -92,4 +93,24 @@ public final class ConfigManager { e.printStackTrace(); } } + + public static int getLastVolume(){ + try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { + librarySettings.load(inputStream); + if (librarySettings.containsKey(lastVolumeKey)) { + return Integer.parseInt(librarySettings.getProperty(lastVolumeKey)); + } + } catch (IOException ignored) { + } + return 100; + } + + public static void setLastVolume(Integer index){ + librarySettings.setProperty(lastVolumeKey, index.toString()); + try(FileWriter fileWriter = new FileWriter(settingsFilename)){ + librarySettings.store(fileWriter, ""); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 3d4fbb4..1ca37cd 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -396,6 +396,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf volumeSlider.setMajorTickSpacing(20); volumeSlider.setMinorTickSpacing(10); volumeSlider.setPaintTicks(true); + player.setVolume(ConfigManager.getLastVolume()); volumeSlider.setValue(player.getVolume()); panel.add(volumeSlider); JLabel volumeValue = new JLabel(); @@ -403,6 +404,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf volumeSlider.addChangeListener(e -> { player.setVolume(((JSlider) e.getSource()).getValue()); volumeValue.setText(String.format("%d%%", player.getVolume())); + ConfigManager.setLastVolume(player.getVolume()); }); panel.add(volumeValue); return panel;