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

View File

@ -21,6 +21,7 @@ import java.util.Random;
*/
public class MPDController implements IMPDController {
private MPD mpd;
private final Object lockObject = new Object();
public MPDController(String host) {
try {
@ -41,9 +42,15 @@ public class MPDController implements IMPDController {
@Override
public void playSong(final String artist, final String 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) { }
try {
MPDPlaylist playlist = mpd.getMPDPlaylist();
@ -67,7 +74,12 @@ public class MPDController implements IMPDController {
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
}
}
}
@ -79,6 +91,9 @@ public class MPDController implements IMPDController {
@Override
public void addToPlayList(final String artist, final String title) {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
@ -100,11 +115,20 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
}
}
}
@Override
public void skipRandomStart() {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
MPDPlayer player = mpd.getMPDPlayer();
try {
@ -121,6 +145,12 @@ public class MPDController implements IMPDController {
e.printStackTrace();
}
}
};
synchronized (lockObject) {
new Thread(r).start();
}
}
}
/**
@ -130,6 +160,10 @@ public class MPDController implements IMPDController {
@Override
public void setVolume(final int volume) {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
mpd.getMPDPlayer().setVolume(volume);
} catch (MPDConnectionException e) {
@ -138,6 +172,12 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
}
}
}
/**
@ -146,6 +186,9 @@ public class MPDController implements IMPDController {
@Override
public void clearPlaylist() {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
MPDPlaylist playlist = mpd.getMPDPlaylist();
playlist.clearPlaylist();
@ -159,12 +202,22 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
}
}
}
private void addToPlayListIfNeeded(final String artist, final String title) {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
int count;
for(count = 0; count < 3; count++) {
try {
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
@ -176,6 +229,8 @@ public class MPDController implements IMPDController {
break;
}
}
return;
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) {
@ -184,5 +239,6 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage());
}
}
}
}