diff --git a/src/main/java/musicplayer/SwingUIModule.java b/src/main/java/musicplayer/SwingUIModule.java index 915f1ad..68d3fbb 100644 --- a/src/main/java/musicplayer/SwingUIModule.java +++ b/src/main/java/musicplayer/SwingUIModule.java @@ -1,20 +1,18 @@ 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; import musicplayer.db.IDatabase; import musicplayer.library.ILibrary; import musicplayer.library.JTreeLibrary; -import musicplayer.player.GStreamerPlayer; import musicplayer.player.IPlayer; -import musicplayer.player.VLCPlayer; import musicplayer.playlist.IPlaylist; import musicplayer.playlist.TabbedJTablePlaylist; import musicplayer.swingui.PlayerGUI; import musicplayer.util.ConfigManager; +import musicplayer.util.Engine; /** * Dependency injection setup using Swing UI Components. @@ -24,19 +22,26 @@ class SwingUIModule extends AbstractModule { @Override protected void configure() { bind(IDatabase.class).to(HibernateDatabase.class).in(Singleton.class); - bind(IPlaylist.class).to(TabbedJTablePlaylist.class).in(Singleton.class); - bind(ILibrary.class).to(JTreeLibrary.class).in(Singleton.class); + bind(IPlaylist.class).to(TabbedJTablePlaylist.class); + bind(ILibrary.class).to(JTreeLibrary.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; + boolean engineSet = false; + for(Engine engine : Engine.values()){ + if(engineName.equals(engine.getEngineName()) && engine.isAvailable()){ + bind(IPlayer.class).to(engine.getEngineImplementation()); + ConfigManager.setPlayerEngine(engine.getEngineName()); + engineSet = true; + } + } + if(!engineSet){ // If the user has set an invalid engine, try and set a usable one. + for(Engine engine : Engine.values()){ + if(engine.isAvailable()){ + bind(IPlayer.class).to(engine.getEngineImplementation()); + ConfigManager.setPlayerEngine(engine.getEngineName()); + break; + } + } } } } diff --git a/src/main/java/musicplayer/util/ConfigManager.java b/src/main/java/musicplayer/util/ConfigManager.java index bc38509..ee21dd2 100644 --- a/src/main/java/musicplayer/util/ConfigManager.java +++ b/src/main/java/musicplayer/util/ConfigManager.java @@ -25,7 +25,7 @@ public final class ConfigManager { private static final String libraryDisplayKey = "libraryDisplayKey"; private static final String lastVolumeKey = "volume"; private static final String engineKey = "engine"; - public static final String[] engineNames = {"GStreamer", "VLC"}; + public static final String[] engineNames = Engine.engineList(); /** * @return List of directories used for music library indexing. diff --git a/src/main/java/musicplayer/util/Engine.java b/src/main/java/musicplayer/util/Engine.java new file mode 100644 index 0000000..e06b3be --- /dev/null +++ b/src/main/java/musicplayer/util/Engine.java @@ -0,0 +1,62 @@ +package musicplayer.util; + +import musicplayer.player.GStreamerPlayer; +import musicplayer.player.IPlayer; +import musicplayer.player.VLCPlayer; + +/** + * Available playback engines. + */ +public enum Engine { + + GSTREAMER("GStreamer", GStreamerPlayer.class, "org.gstreamer.elements.PlayBin2"), + VLC("VLC", VLCPlayer.class, "uk.co.caprica.vlcj.player.MediaPlayer"); + + private final String engineName; + private final Class engineImplementation; + private final String neededClass; + + Engine(String engineName, Class engineImplementation, String neededClass) { + this.engineName = engineName; + this.engineImplementation = engineImplementation; + this.neededClass = neededClass; + } + + /** + * @return Name of the engine. + */ + public String getEngineName() { + return engineName; + } + + /** + * @return Class used as the implementation for this engine. + */ + public Class getEngineImplementation() { + return engineImplementation; + } + + /** + * @return List of all available engines. + */ + public static String[] engineList(){ + String[] engineList = new String[Engine.values().length]; + for(int i = 0; i < engineList.length; i++){ + engineList[i] = Engine.values()[i].getEngineName(); + } + return engineList; + } + + /** + * @return Whether the needed library for this playback engine is available. + */ + public boolean isAvailable() { + try { + Class.forName(neededClass); + return true; + } catch (ClassNotFoundException ignored) { + System.out.println("Couldn't load " + neededClass); + } + return false; + } +}