Changed dependency injection of player implementation so that a missing library falls back to another engine.

This commit is contained in:
neviyn 2016-12-04 19:36:39 +00:00
parent 49055daf4b
commit 79ee3d3ff2
3 changed files with 82 additions and 15 deletions

View File

@ -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;
}
}
}
}
}

View File

@ -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.

View File

@ -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<? extends IPlayer> engineImplementation;
private final String neededClass;
Engine(String engineName, Class<? extends IPlayer> 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<? extends IPlayer> 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;
}
}