Changed dependency injection of player implementation so that a missing library falls back to another engine.
This commit is contained in:
parent
49055daf4b
commit
79ee3d3ff2
@ -1,20 +1,18 @@
|
|||||||
package musicplayer;
|
package musicplayer;
|
||||||
|
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.Provides;
|
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
import musicplayer.callbacks.PlayerCallbackInterface;
|
import musicplayer.callbacks.PlayerCallbackInterface;
|
||||||
import musicplayer.db.HibernateDatabase;
|
import musicplayer.db.HibernateDatabase;
|
||||||
import musicplayer.db.IDatabase;
|
import musicplayer.db.IDatabase;
|
||||||
import musicplayer.library.ILibrary;
|
import musicplayer.library.ILibrary;
|
||||||
import musicplayer.library.JTreeLibrary;
|
import musicplayer.library.JTreeLibrary;
|
||||||
import musicplayer.player.GStreamerPlayer;
|
|
||||||
import musicplayer.player.IPlayer;
|
import musicplayer.player.IPlayer;
|
||||||
import musicplayer.player.VLCPlayer;
|
|
||||||
import musicplayer.playlist.IPlaylist;
|
import musicplayer.playlist.IPlaylist;
|
||||||
import musicplayer.playlist.TabbedJTablePlaylist;
|
import musicplayer.playlist.TabbedJTablePlaylist;
|
||||||
import musicplayer.swingui.PlayerGUI;
|
import musicplayer.swingui.PlayerGUI;
|
||||||
import musicplayer.util.ConfigManager;
|
import musicplayer.util.ConfigManager;
|
||||||
|
import musicplayer.util.Engine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dependency injection setup using Swing UI Components.
|
* Dependency injection setup using Swing UI Components.
|
||||||
@ -24,19 +22,26 @@ class SwingUIModule extends AbstractModule {
|
|||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
bind(IDatabase.class).to(HibernateDatabase.class).in(Singleton.class);
|
bind(IDatabase.class).to(HibernateDatabase.class).in(Singleton.class);
|
||||||
bind(IPlaylist.class).to(TabbedJTablePlaylist.class).in(Singleton.class);
|
bind(IPlaylist.class).to(TabbedJTablePlaylist.class);
|
||||||
bind(ILibrary.class).to(JTreeLibrary.class).in(Singleton.class);
|
bind(ILibrary.class).to(JTreeLibrary.class);
|
||||||
bind(PlayerCallbackInterface.class).to(PlayerGUI.class);
|
bind(PlayerCallbackInterface.class).to(PlayerGUI.class);
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
private IPlayer providePlayer(GStreamerPlayer gStreamerPlayer, VLCPlayer vlcPlayer){
|
|
||||||
String engineName = ConfigManager.getPlayerEngine();
|
String engineName = ConfigManager.getPlayerEngine();
|
||||||
switch (engineName){
|
boolean engineSet = false;
|
||||||
case "GStreamer": return gStreamerPlayer;
|
for(Engine engine : Engine.values()){
|
||||||
case "VLC": return vlcPlayer;
|
if(engineName.equals(engine.getEngineName()) && engine.isAvailable()){
|
||||||
default: return gStreamerPlayer;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ public final class ConfigManager {
|
|||||||
private static final String libraryDisplayKey = "libraryDisplayKey";
|
private static final String libraryDisplayKey = "libraryDisplayKey";
|
||||||
private static final String lastVolumeKey = "volume";
|
private static final String lastVolumeKey = "volume";
|
||||||
private static final String engineKey = "engine";
|
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.
|
* @return List of directories used for music library indexing.
|
||||||
|
62
src/main/java/musicplayer/util/Engine.java
Normal file
62
src/main/java/musicplayer/util/Engine.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user