diff --git a/src/main/java/musicplayer/SwingUIModule.java b/src/main/java/musicplayer/SwingUIModule.java index d258d43..d15e0b8 100644 --- a/src/main/java/musicplayer/SwingUIModule.java +++ b/src/main/java/musicplayer/SwingUIModule.java @@ -1,6 +1,7 @@ package musicplayer; import com.google.inject.AbstractModule; +import com.google.inject.Provides; import com.google.inject.Singleton; import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.db.HibernateDatabase; @@ -13,15 +14,26 @@ import musicplayer.player.VLCPlayer; import musicplayer.playlist.IPlaylist; import musicplayer.playlist.JTablePlaylist; import musicplayer.swingui.PlayerGUI; +import musicplayer.util.ConfigManager; class SwingUIModule extends AbstractModule { @Override protected void configure() { - bind(IPlayer.class).to(GStreamerPlayer.class).in(Singleton.class); bind(IDatabase.class).to(HibernateDatabase.class).in(Singleton.class); bind(IPlaylist.class).to(JTablePlaylist.class).in(Singleton.class); bind(ILibrary.class).to(JTreeLibrary.class).in(Singleton.class); bind(PlayerCallbackInterface.class).to(PlayerGUI.class); } + + @Provides + @Singleton + private IPlayer providePlayer(GStreamerPlayer gStreamerPlayer, VLCPlayer vlcPlayer){ + String engineName = ConfigManager.getPlayerEngine(); + switch (engineName){ + case "GStreamer": return gStreamerPlayer; + case "VLC": return vlcPlayer; + default: return gStreamerPlayer; + } + } } diff --git a/src/main/java/musicplayer/player/VLCPlayer.java b/src/main/java/musicplayer/player/VLCPlayer.java index a678774..3b6ffae 100644 --- a/src/main/java/musicplayer/player/VLCPlayer.java +++ b/src/main/java/musicplayer/player/VLCPlayer.java @@ -189,9 +189,14 @@ public class VLCPlayer implements IPlayer { @Override public void playing(MediaPlayer mediaPlayer) { - mediaPlayer.setVolume(currentVolume); if(callbackInterface != null) callbackInterface.setSeekBarDuration((int) (mediaPlayer.getLength() / 1000)); + while(true){ // Can't change volume till it is *actually* playing + if (mediaPlayer.getTime() != 0){ + mediaPlayer.setVolume(currentVolume); + break; + } + } } @Override diff --git a/src/main/java/musicplayer/swingui/LibraryConfigGUI.java b/src/main/java/musicplayer/swingui/LibraryConfigGUI.java index c3fee9f..cd610e7 100644 --- a/src/main/java/musicplayer/swingui/LibraryConfigGUI.java +++ b/src/main/java/musicplayer/swingui/LibraryConfigGUI.java @@ -12,6 +12,7 @@ class LibraryConfigGUI { private JList listLibraryFolders; private final LibraryListModel listModel = new LibraryListModel<>(); private JPanel mainPanel; + private static final String[] engineNames = {"GStreamer", "VLC"}; public LibraryConfigGUI(){ JFrame frame = new JFrame(); @@ -44,9 +45,23 @@ class LibraryConfigGUI { removeButton.setText("Remove"); removeButton.addActionListener(e -> removeSelected()); panel2.add(removeButton, constraints); + mainPanel.add(engineConfigUI(), BorderLayout.SOUTH); return mainPanel; } + private JPanel engineConfigUI() { + JPanel container = new JPanel(new BorderLayout()); + container.add(new JLabel("Select Engine: "), BorderLayout.WEST); + JComboBox comboBox = new JComboBox<>(engineNames); + comboBox.setSelectedItem(ConfigManager.getPlayerEngine()); + comboBox.addActionListener(e -> { + ConfigManager.setPlayerEngine((String)comboBox.getSelectedItem()); + JOptionPane.showMessageDialog(mainPanel, "Engine changes will be applied on restart."); + }); + container.add(comboBox, BorderLayout.CENTER); + return container; + } + private void updateLibraryListContents(){ listModel.setFolderList(ConfigManager.getLibraryDirectories()); } diff --git a/src/main/java/musicplayer/util/ConfigManager.java b/src/main/java/musicplayer/util/ConfigManager.java index 6401aa5..53671e3 100644 --- a/src/main/java/musicplayer/util/ConfigManager.java +++ b/src/main/java/musicplayer/util/ConfigManager.java @@ -20,6 +20,7 @@ public final class ConfigManager { private static final String databaseKey = "databaseDir"; private static final String libraryDisplayKey = "libraryDisplayKey"; private static final String lastVolumeKey = "volume"; + private static final String engineKey = "engine"; /** * @return List of directories used for music library indexing. @@ -92,7 +93,7 @@ public final class ConfigManager { } } catch (IOException ignored) { } - return 100; + return 0; } public static void setLastVolume(Integer index){ @@ -100,8 +101,24 @@ public final class ConfigManager { writeSettings(); } - private static void writeSettings(){ - try(FileWriter fileWriter = new FileWriter(settingsFilename)){ + public static String getPlayerEngine(){ + try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { + librarySettings.load(inputStream); + if (librarySettings.containsKey(engineKey)) { + return librarySettings.getProperty(engineKey); + } + } catch (IOException ignored) { + } + return "GStreamer"; + } + + public static void setPlayerEngine(String engineName){ + librarySettings.setProperty(engineKey, engineName); + writeSettings(); + } + + private static void writeSettings() { + try (FileWriter fileWriter = new FileWriter(settingsFilename)) { librarySettings.store(fileWriter, ""); } catch (IOException e) { e.printStackTrace(); diff --git a/src/test/java/musicplayer/player/VLCPlayerTest.java b/src/test/java/musicplayer/player/VLCPlayerTest.java index db22d3e..859de01 100644 --- a/src/test/java/musicplayer/player/VLCPlayerTest.java +++ b/src/test/java/musicplayer/player/VLCPlayerTest.java @@ -4,6 +4,7 @@ import musicplayer.library.ILibrary; import musicplayer.model.Song; import musicplayer.playlist.IPlaylist; import musicplayer.playlist.JTablePlaylist; +import musicplayer.util.ConfigManager; import org.junit.Before; import org.junit.Test; @@ -66,12 +67,19 @@ public class VLCPlayerTest { assertFalse(player.isPlaying()); } + @Test + public void testSetVolume() throws Exception { + assertEquals(ConfigManager.getLastVolume(), player.getVolume()); + int newVolume = 25; + player.setVolume(25); + assertEquals(newVolume, player.getVolume()); + } + @Test public void testSeek() throws Exception { player.playSong(Optional.of(sampleSong)); assertTrue(player.isPlaying()); player.seek(100); - System.out.println(player.currentSongPosition()); assertTrue(player.currentSongPosition() >= 100); } } \ No newline at end of file