changed mpd back to thread and fixed playlist handling

This commit is contained in:
Lucas Pleß 2012-06-19 21:37:01 +02:00
parent 54fd08440b
commit 2a40f0971e
2 changed files with 131 additions and 84 deletions

View File

@ -65,7 +65,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
case ENTERED_ROOM: case ENTERED_ROOM:
relaisboard.setRelais(7, false); // disable light barrier over relais relaisboard.setRelais(7, false); // disable light barrier over relais
mpdController.playSong("crashtest", "entered_room"); mpdController.playSong("crashtest", "entered_room");
mpdController.setVolume(70);
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
bunti.setPar56(20,0,100); bunti.setPar56(20,0,100);
@ -79,7 +78,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
relaisboard.setRelais(6, true); // enable third green circle relaisboard.setRelais(6, true); // enable third green circle
mpdController.playSong("crashtest", "table_game_one"); mpdController.playSong("crashtest", "table_game_one");
mpdController.setVolume(70);
bunti.setLampel(true,false,false); bunti.setLampel(true,false,false);
bunti.setPar56(20,0,100); bunti.setPar56(20,0,100);
@ -87,7 +85,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case TABLE_GAME_TWO: case TABLE_GAME_TWO:
mpdController.playSong("crashtest", "table_game_two"); mpdController.playSong("crashtest", "table_game_two");
mpdController.setVolume(67);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(100,0,100); bunti.setPar56(100,0,100);
@ -96,7 +93,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case TABLE_GAME_THREE: case TABLE_GAME_THREE:
mpdController.playSong("crashtest", "table_game_three"); mpdController.playSong("crashtest", "table_game_three");
mpdController.setVolume(63);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,35,0); bunti.setPar56(255,35,0);
@ -105,7 +101,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case TABLE_GAME_FOUR: case TABLE_GAME_FOUR:
mpdController.playSong("crashtest", "table_game_four"); mpdController.playSong("crashtest", "table_game_four");
mpdController.setVolume(60);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(200,100,0); bunti.setPar56(200,100,0);
@ -114,7 +109,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case TABLE_GAME_FIVE: case TABLE_GAME_FIVE:
mpdController.playSong("crashtest", "table_game_five"); mpdController.playSong("crashtest", "table_game_five");
mpdController.setVolume(57);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(150,150,0); bunti.setPar56(150,150,0);
@ -123,7 +117,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case TABLE_GAME_SIX: case TABLE_GAME_SIX:
mpdController.playSong("crashtest", "table_game_six"); mpdController.playSong("crashtest", "table_game_six");
mpdController.setVolume(53);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(100,200,0); bunti.setPar56(100,200,0);
@ -132,7 +125,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case TABLE_GAME_DONE: case TABLE_GAME_DONE:
mpdController.playSong("crashtest", "table_game_done"); mpdController.playSong("crashtest", "table_game_done");
mpdController.setVolume(50);
bunti.setLampel(false,false,true); bunti.setLampel(false,false,true);
bunti.setPar56(100,255,0); bunti.setPar56(100,255,0);
@ -160,7 +152,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
case ROKET_STARTED: case ROKET_STARTED:
mpdController.playSong("crashtest", "roket_started"); mpdController.playSong("crashtest", "roket_started");
mpdController.addToPlayList("crashtest", "roket_started2"); mpdController.addToPlayList("crashtest", "roket_started2");
mpdController.setVolume(50);
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(0, 255, 0); bunti.setPar56(0, 255, 0);
@ -176,7 +167,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
break; break;
case ROKET_DONE: case ROKET_DONE:
mpdController.playSong("crashtest", "roket_done"); mpdController.playSong("crashtest", "roket_done");
mpdController.setVolume(60); //mpdController.setVolume(60);
bunti.setLampel(false,false,true); bunti.setLampel(false,false,true);
bunti.setPar56(255, 255, 255); bunti.setPar56(255, 255, 255);

View File

@ -21,6 +21,7 @@ import java.util.Random;
*/ */
public class MPDController implements IMPDController { public class MPDController implements IMPDController {
private MPD mpd; private MPD mpd;
private final Object lockObject = new Object();
public MPDController(String host) { public MPDController(String host) {
try { try {
@ -41,33 +42,44 @@ public class MPDController implements IMPDController {
@Override @Override
public void playSong(final String artist, final String title) { public void playSong(final String artist, final String title) {
System.out.println("playSong: " + artist + " - " + title); System.out.println("playSong: " + artist + " - " + title);
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
addToPlayListIfNeeded(artist, title);
try {
Thread.sleep(500);
} catch (InterruptedException e) { }
addToPlayListIfNeeded(artist, title); try {
MPDPlaylist playlist = mpd.getMPDPlaylist();
try { for(MPDSong song: playlist.getSongList()) {
MPDPlaylist playlist = mpd.getMPDPlaylist();
for(MPDSong song: playlist.getSongList()) { if(song.getArtist() != null && song.getTitle() != null) {
if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) &&
song.getTitle().toLowerCase().equals(title.toLowerCase())) {
if(song.getArtist() != null && song.getTitle() != null) { MPDPlayer player = mpd.getMPDPlayer();
if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) && player.stop();
song.getTitle().toLowerCase().equals(title.toLowerCase())) { player.playId(song);
break;
MPDPlayer player = mpd.getMPDPlayer(); }
player.stop(); }
player.playId(song);
break;
} }
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
};
} catch (MPDConnectionException e) { synchronized (lockObject) {
Logger.sLog("MPD error: " + e.getMessage()); new Thread(r).start();
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
} }
@ -79,25 +91,34 @@ public class MPDController implements IMPDController {
@Override @Override
public void addToPlayList(final String artist, final String title) { public void addToPlayList(final String artist, final String title) {
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
MPDDatabase db = mpd.getMPDDatabase(); MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
try { try {
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist)); List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
for(MPDSong song: tracks) { for(MPDSong song: tracks) {
if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) { if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
playlist.addSong(song); playlist.addSong(song);
break; break;
}
}
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
} catch (MPDConnectionException e) { };
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) { synchronized (lockObject) {
Logger.sLog("MPD error: " + e.getMessage()); new Thread(r).start();
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
} }
@ -105,20 +126,29 @@ public class MPDController implements IMPDController {
@Override @Override
public void skipRandomStart() { public void skipRandomStart() {
if(mpd != null) { if(mpd != null) {
MPDPlayer player = mpd.getMPDPlayer(); Runnable r = new Runnable() {
@Override
public void run() {
MPDPlayer player = mpd.getMPDPlayer();
try { try {
int length; int length;
MPDSong song = player.getCurrentSong(); MPDSong song = player.getCurrentSong();
if(song!= null) { if(song!= null) {
length = song.getLength(); length = song.getLength();
int skip = new Random().nextInt(length/2); int skip = new Random().nextInt(length/2);
player.seek(skip); player.seek(skip);
}
} catch (MPDConnectionException e) {
e.printStackTrace();
} catch (MPDPlayerException e) {
e.printStackTrace();
}
} }
} catch (MPDConnectionException e) { };
e.printStackTrace();
} catch (MPDPlayerException e) { synchronized (lockObject) {
e.printStackTrace(); new Thread(r).start();
} }
} }
} }
@ -130,12 +160,22 @@ public class MPDController implements IMPDController {
@Override @Override
public void setVolume(final int volume) { public void setVolume(final int volume) {
if(mpd != null) { if(mpd != null) {
try { Runnable r = new Runnable() {
mpd.getMPDPlayer().setVolume(volume); @Override
} catch (MPDConnectionException e) { public void run() {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) { try {
Logger.sLog("MPD error: " + e.getMessage()); mpd.getMPDPlayer().setVolume(volume);
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
} }
} }
} }
@ -146,17 +186,26 @@ public class MPDController implements IMPDController {
@Override @Override
public void clearPlaylist() { public void clearPlaylist() {
if(mpd != null) { if(mpd != null) {
try { Runnable r = new Runnable() {
MPDPlaylist playlist = mpd.getMPDPlaylist(); @Override
playlist.clearPlaylist(); public void run() {
mpd.getMPDPlayer().setRandom(false); try {
mpd.getMPDPlayer().setXFade(1); MPDPlaylist playlist = mpd.getMPDPlaylist();
} catch (MPDConnectionException e) { playlist.clearPlaylist();
Logger.sLog("MPD error: " + e.getMessage()); mpd.getMPDPlayer().setRandom(false);
} catch (MPDPlaylistException e) { mpd.getMPDPlayer().setXFade(1);
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDConnectionException e) {
} catch (MPDPlayerException e) { Logger.sLog("MPD error: " + e.getMessage());
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
} }
} }
} }
@ -165,23 +214,30 @@ public class MPDController implements IMPDController {
MPDDatabase db = mpd.getMPDDatabase(); MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
try { int count;
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
for(MPDSong song: tracks) { for(count = 0; count < 3; count++) {
if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
if(!playlist.getSongList().contains(song)) { try {
playlist.addSong(song); List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
for(MPDSong song: tracks) {
if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
if(!playlist.getSongList().contains(song)) {
playlist.addSong(song);
}
break;
} }
break;
} }
return;
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }