Playback engine can be changed in the application. Fix VLC backend not applying volume settings on first run.

This commit is contained in:
neviyn 2016-04-02 01:49:04 +01:00
parent bacb560f6c
commit e83c08f206
5 changed files with 63 additions and 6 deletions

View File

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

View File

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

View File

@ -12,6 +12,7 @@ class LibraryConfigGUI {
private JList<File> listLibraryFolders;
private final LibraryListModel<File> 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<String> 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());
}

View File

@ -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();

View File

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